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

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/patest_sine8.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_sine8.c
2
 
        @ingroup test_src
3
 
        @brief Test 8 bit data: play a sine wave for several seconds.
4
 
        @author Ross Bencina <rossb@audiomulch.com>
5
 
*/
6
 
/*
7
 
 * $Id: patest_sine8.c,v 1.2 2006/09/23 18:42:52 llucius Exp $
8
 
 *
9
 
 * This program uses the PortAudio Portable Audio Library.
10
 
 * For more information see: http://www.portaudio.com
11
 
 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
12
 
 *
13
 
 * Permission is hereby granted, free of charge, to any person obtaining
14
 
 * a copy of this software and associated documentation files
15
 
 * (the "Software"), to deal in the Software without restriction,
16
 
 * including without limitation the rights to use, copy, modify, merge,
17
 
 * publish, distribute, sublicense, and/or sell copies of the Software,
18
 
 * and to permit persons to whom the Software is furnished to do so,
19
 
 * subject to the following conditions:
20
 
 *
21
 
 * The above copyright notice and this permission notice shall be
22
 
 * included in all copies or substantial portions of the Software.
23
 
 *
24
 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25
 
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27
 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
28
 
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29
 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30
 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
 
 */
32
 
 
33
 
/*
34
 
 * The text above constitutes the entire PortAudio license; however, 
35
 
 * the PortAudio community also makes the following non-binding requests:
36
 
 *
37
 
 * Any person wishing to distribute modifications to the Software is
38
 
 * requested to send the modifications to the original developer so that
39
 
 * they can be incorporated into the canonical version. It is also 
40
 
 * requested that these non-binding requests be included along with the 
41
 
 * license above.
42
 
 */
43
 
 
44
 
#include <stdio.h>
45
 
#include <math.h>
46
 
#include "portaudio.h"
47
 
 
48
 
#define NUM_SECONDS   (8)
49
 
#define SAMPLE_RATE   (44100)
50
 
#define TABLE_SIZE    (200)
51
 
#define TEST_UNSIGNED (0)
52
 
 
53
 
#if TEST_UNSIGNED
54
 
#define TEST_FORMAT   paUInt8
55
 
#else
56
 
#define TEST_FORMAT   paInt8
57
 
#endif
58
 
 
59
 
#ifndef M_PI
60
 
#define M_PI (3.14159265)
61
 
#endif
62
 
 
63
 
typedef struct
64
 
{
65
 
#if TEST_UNSIGNED
66
 
    unsigned char sine[TABLE_SIZE];
67
 
#else
68
 
    char sine[TABLE_SIZE];
69
 
#endif
70
 
    int left_phase;
71
 
    int right_phase;
72
 
    unsigned int framesToGo;
73
 
}
74
 
paTestData;
75
 
 
76
 
/* This routine will be called by the PortAudio engine when audio is needed.
77
 
** It may called at interrupt level on some machines so don't do anything
78
 
** that could mess up the system like calling malloc() or free().
79
 
*/
80
 
static int patestCallback( const void *inputBuffer, void *outputBuffer,
81
 
                           unsigned long framesPerBuffer,
82
 
                           const PaStreamCallbackTimeInfo* timeInfo,
83
 
                           PaStreamCallbackFlags statusFlags,
84
 
                           void *userData )
85
 
{
86
 
    paTestData *data = (paTestData*)userData;
87
 
    char *out = (char*)outputBuffer;
88
 
    int i;
89
 
    int framesToCalc;
90
 
    int finished = 0;
91
 
    (void) inputBuffer; /* Prevent unused variable warnings. */
92
 
 
93
 
    if( data->framesToGo < framesPerBuffer )
94
 
    {
95
 
        framesToCalc = data->framesToGo;
96
 
        data->framesToGo = 0;
97
 
        finished = 1;
98
 
    }
99
 
    else
100
 
    {
101
 
        framesToCalc = framesPerBuffer;
102
 
        data->framesToGo -= framesPerBuffer;
103
 
    }
104
 
 
105
 
    for( i=0; i<framesToCalc; i++ )
106
 
    {
107
 
        *out++ = data->sine[data->left_phase];  /* left */
108
 
        *out++ = data->sine[data->right_phase];  /* right */
109
 
        data->left_phase += 1;
110
 
        if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
111
 
        data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
112
 
        if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
113
 
    }
114
 
    /* zero remainder of final buffer */
115
 
    for( ; i<(int)framesPerBuffer; i++ )
116
 
    {
117
 
#if TEST_UNSIGNED
118
 
        *out++ = (unsigned char) 0x80; /* left */
119
 
        *out++ = (unsigned char) 0x80; /* right */
120
 
#else
121
 
        *out++ = 0; /* left */
122
 
        *out++ = 0; /* right */
123
 
#endif
124
 
 
125
 
    }
126
 
    return finished;
127
 
}
128
 
 
129
 
/*******************************************************************/
130
 
int main(void);
131
 
int main(void)
132
 
{
133
 
    PaStreamParameters  outputParameters;
134
 
    PaStream*           stream;
135
 
    PaError             err;
136
 
    paTestData          data;
137
 
    PaTime              streamOpened;
138
 
    int                 i, totalSamps;
139
 
 
140
 
#if TEST_UNSIGNED
141
 
    printf("PortAudio Test: output UNsigned 8 bit sine wave.\n");
142
 
#else
143
 
    printf("PortAudio Test: output signed 8 bit sine wave.\n");
144
 
#endif
145
 
    /* initialise sinusoidal wavetable */
146
 
    for( i=0; i<TABLE_SIZE; i++ )
147
 
    {
148
 
        data.sine[i] = (char) (127.0 * sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. ));
149
 
#if TEST_UNSIGNED
150
 
        data.sine[i] += (unsigned char) 0x80;
151
 
#endif
152
 
    }
153
 
    data.left_phase = data.right_phase = 0;
154
 
    data.framesToGo = totalSamps =  NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
155
 
 
156
 
    err = Pa_Initialize();
157
 
    if( err != paNoError )
158
 
        goto error;
159
 
 
160
 
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* Default output device. */
161
 
    outputParameters.channelCount = 2;                     /* Stereo output. */
162
 
    outputParameters.sampleFormat = TEST_FORMAT;
163
 
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
164
 
    outputParameters.hostApiSpecificStreamInfo = NULL;
165
 
    err = Pa_OpenStream( &stream,
166
 
                         NULL,      /* No input. */
167
 
                         &outputParameters,
168
 
                         SAMPLE_RATE,
169
 
                         256,       /* Frames per buffer. */
170
 
                         paClipOff, /* We won't output out of range samples so don't bother clipping them. */
171
 
                         patestCallback,
172
 
                         &data );
173
 
    if( err != paNoError )
174
 
        goto error;
175
 
 
176
 
    streamOpened = Pa_GetStreamTime( stream ); /* Time in seconds when stream was opened (approx). */
177
 
 
178
 
    err = Pa_StartStream( stream );
179
 
    if( err != paNoError )
180
 
        goto error;
181
 
 
182
 
    /* Watch until sound is halfway finished. */
183
 
    /* (Was ( Pa_StreamTime( stream ) < (totalSamps/2) ) in V18. */
184
 
    while( (Pa_GetStreamTime( stream ) - streamOpened) < (PaTime)NUM_SECONDS / 2.0 )
185
 
        Pa_Sleep(10);
186
 
 
187
 
    /* Stop sound until ENTER hit. (Hu? don't see any keyboard-input here.) */
188
 
    err = Pa_StopStream( stream );
189
 
    if( err != paNoError )
190
 
        goto error;
191
 
 
192
 
    printf("Pause for 2 seconds.\n");
193
 
    Pa_Sleep( 2000 );
194
 
 
195
 
    err = Pa_StartStream( stream );
196
 
    if( err != paNoError )
197
 
        goto error;
198
 
 
199
 
    printf("Waiting for sound to finish.\n");
200
 
 
201
 
    while( ( err = Pa_IsStreamActive( stream ) ) == 1 )
202
 
        Pa_Sleep(100);
203
 
    if( err < 0 )
204
 
        goto error;
205
 
 
206
 
    err = Pa_CloseStream( stream );
207
 
    if( err != paNoError )
208
 
        goto error;
209
 
 
210
 
    Pa_Terminate();
211
 
    printf("Test finished.\n");
212
 
    return err;
213
 
error:
214
 
    Pa_Terminate();
215
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
216
 
    fprintf( stderr, "Error number: %d\n", err );
217
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
218
 
    return err;
219
 
}