~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest_two_rates.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** @file patest_two_rates.c
2
 
        @ingroup test_src
3
 
        @brief Play two streams at different rates to make sure they don't interfere.
4
 
        @author Phil Burk <philburk@softsynth.com>
5
 
*/
6
 
/*
7
 
 * $Id: patest_two_rates.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
8
 
 *
9
 
 * Author: Phil Burk  http://www.softsynth.com
10
 
 *
11
 
 * This program uses the PortAudio Portable Audio Library.
12
 
 * For more information see: http://www.portaudio.com
13
 
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
14
 
 *
15
 
 * Permission is hereby granted, free of charge, to any person obtaining
16
 
 * a copy of this software and associated documentation files
17
 
 * (the "Software"), to deal in the Software without restriction,
18
 
 * including without limitation the rights to use, copy, modify, merge,
19
 
 * publish, distribute, sublicense, and/or sell copies of the Software,
20
 
 * and to permit persons to whom the Software is furnished to do so,
21
 
 * subject to the following conditions:
22
 
 *
23
 
 * The above copyright notice and this permission notice shall be
24
 
 * included in all copies or substantial portions of the Software.
25
 
 *
26
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27
 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28
 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
29
 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
30
 
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
31
 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32
 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33
 
 */
34
 
 
35
 
/*
36
 
 * The text above constitutes the entire PortAudio license; however, 
37
 
 * the PortAudio community also makes the following non-binding requests:
38
 
 *
39
 
 * Any person wishing to distribute modifications to the Software is
40
 
 * requested to send the modifications to the original developer so that
41
 
 * they can be incorporated into the canonical version. It is also 
42
 
 * requested that these non-binding requests be included along with the 
43
 
 * license above.
44
 
 */
45
 
 
46
 
#include <stdio.h>
47
 
#include <math.h>
48
 
#include "portaudio.h"
49
 
 
50
 
#define OUTPUT_DEVICE       (Pa_GetDefaultOutputDeviceID())
51
 
#define SAMPLE_RATE_1       (44100)
52
 
#define SAMPLE_RATE_2       (44100)
53
 
#define FRAMES_PER_BUFFER   (256)
54
 
#define FREQ_INCR           (0.1)
55
 
 
56
 
#ifndef M_PI
57
 
#define M_PI  (3.14159265)
58
 
#endif
59
 
 
60
 
typedef struct
61
 
{
62
 
    double phase;
63
 
    double numFrames;
64
 
} paTestData;
65
 
 
66
 
/* This routine will be called by the PortAudio engine when audio is needed.
67
 
** It may called at interrupt level on some machines so don't do anything
68
 
** that could mess up the system like calling malloc() or free().
69
 
*/
70
 
static int patestCallback( void *inputBuffer, void *outputBuffer,
71
 
                           unsigned long framesPerBuffer,
72
 
                           PaTimestamp outTime, void *userData )
73
 
{
74
 
    paTestData *data = (paTestData*)userData;
75
 
    float *out = (float*)outputBuffer;
76
 
    int frameIndex, channelIndex;
77
 
    int finished = 0;
78
 
    (void) outTime; /* Prevent unused variable warnings. */
79
 
    (void) inputBuffer;
80
 
 
81
 
    for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ )
82
 
    {
83
 
        /* Generate sine wave. */
84
 
        float value = (float) 0.3 * sin(data->phase);
85
 
        /* Stereo - two channels. */
86
 
        *out++ = value;
87
 
        *out++ = value;
88
 
 
89
 
        data->phase += FREQ_INCR;
90
 
        if( data->phase >= (2.0 * M_PI) ) data->phase -= (2.0 * M_PI);
91
 
    }
92
 
 
93
 
    return 0;
94
 
}
95
 
 
96
 
/*******************************************************************/
97
 
int main(void);
98
 
int main(void)
99
 
{
100
 
    PaError err;
101
 
    PortAudioStream *stream1;
102
 
    PortAudioStream *stream2;
103
 
    paTestData data1 = {0};
104
 
    paTestData data2 = {0};
105
 
    printf("PortAudio Test: two rates.\n" );
106
 
 
107
 
    err = Pa_Initialize();
108
 
    if( err != paNoError ) goto error;
109
 
    
110
 
    /* Start first stream. **********************/
111
 
    err = Pa_OpenStream(
112
 
              &stream1,
113
 
              paNoDevice, /* default input device */
114
 
              0,              /* no input */
115
 
              paFloat32,  /* 32 bit floating point input */
116
 
              NULL,
117
 
              OUTPUT_DEVICE,
118
 
              2,          /* Stereo */
119
 
              paFloat32,  /* 32 bit floating point output */
120
 
              NULL,
121
 
              SAMPLE_RATE_1,
122
 
              FRAMES_PER_BUFFER,  /* frames per buffer */
123
 
              0,    /* number of buffers, if zero then use default minimum */
124
 
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
125
 
              patestCallback,
126
 
              &data1 );
127
 
    if( err != paNoError ) goto error;
128
 
 
129
 
    err = Pa_StartStream( stream1 );
130
 
    if( err != paNoError ) goto error;
131
 
 
132
 
    Pa_Sleep( 3 * 1000 );
133
 
 
134
 
    /* Start second stream. **********************/
135
 
    err = Pa_OpenStream(
136
 
              &stream2,
137
 
              paNoDevice, /* default input device */
138
 
              0,              /* no input */
139
 
              paFloat32,  /* 32 bit floating point input */
140
 
              NULL,
141
 
              OUTPUT_DEVICE,
142
 
              2,          /* Stereo */
143
 
              paFloat32,  /* 32 bit floating point output */
144
 
              NULL,
145
 
              SAMPLE_RATE_2,
146
 
              FRAMES_PER_BUFFER,  /* frames per buffer */
147
 
              0,    /* number of buffers, if zero then use default minimum */
148
 
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
149
 
              patestCallback,
150
 
              &data2 );
151
 
    if( err != paNoError ) goto error;
152
 
 
153
 
    err = Pa_StartStream( stream2 );
154
 
    if( err != paNoError ) goto error;
155
 
 
156
 
    Pa_Sleep( 3 * 1000 );
157
 
    
158
 
    err = Pa_StopStream( stream2 );
159
 
    if( err != paNoError ) goto error;
160
 
 
161
 
    Pa_Sleep( 3 * 1000 );
162
 
    
163
 
    err = Pa_StopStream( stream1 );
164
 
    if( err != paNoError ) goto error;
165
 
 
166
 
    Pa_CloseStream( stream2 );
167
 
    Pa_CloseStream( stream1 );
168
 
    
169
 
    Pa_Terminate();
170
 
    printf("Test finished.\n");
171
 
    return err;
172
 
error:
173
 
    Pa_Terminate();
174
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
175
 
    fprintf( stderr, "Error number: %d\n", err );
176
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
177
 
    return err;
178
 
}