source: portaudio/branches/V18.1/pa_tests/patest_latency.c @ 315

Revision 315, 6.2 KB checked in by pieter, 8 years ago (diff)

Changed some C++ comments () to C comments (//).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * $Id$
3 * Hear the latency caused by big buffers.
4 * Play a sine wave and change frequency based on letter input.
5 *
6 * Author: Phil Burk <philburk@softsynth.com>, and Darren Gibbs
7 *
8 * This program uses the PortAudio Portable Audio Library.
9 * For more information see: http://www.portaudio.com
10 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files
14 * (the "Software"), to deal in the Software without restriction,
15 * including without limitation the rights to use, copy, modify, merge,
16 * publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so,
18 * subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
22 *
23 * Any person wishing to distribute modifications to the Software is
24 * requested to send the modifications to the original developer so that
25 * they can be incorporated into the canonical version.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
30 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
31 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 *
35 */
36#include <stdio.h>
37#include <math.h>
38#include "portaudio.h"
39
40#define OUTPUT_DEVICE       (Pa_GetDefaultOutputDeviceID())
41#define SAMPLE_RATE         (44100)
42#define FRAMES_PER_BUFFER   (64)
43
44#if 0
45#define MIN_LATENCY_MSEC    (2000)
46#define NUM_BUFFERS         ((MIN_LATENCY_MSEC * SAMPLE_RATE) / (FRAMES_PER_BUFFER * 1000))
47#else
48#define NUM_BUFFERS         (0)
49#endif
50
51#define MIN_FREQ            (100.0f)
52#define CalcPhaseIncrement(freq)  ((freq)/SAMPLE_RATE)
53#ifndef M_PI
54#define M_PI  (3.14159265)
55#endif
56#define TABLE_SIZE   (400)
57typedef struct
58{
59    float sine[TABLE_SIZE + 1]; /* add one for guard point for interpolation */
60    float phase_increment;
61    float left_phase;
62    float right_phase;
63}
64paTestData;
65float LookupSine( paTestData *data, float phase );
66/* Convert phase between and 1.0 to sine value
67 * using linear interpolation.
68 */
69float LookupSine( paTestData *data, float phase )
70{
71    float fIndex = phase*TABLE_SIZE;
72    int   index = (int) fIndex;
73    float fract = fIndex - index;
74    float lo = data->sine[index];
75    float hi = data->sine[index+1];
76    float val = lo + fract*(hi-lo);
77    return val;
78}
79/* This routine will be called by the PortAudio engine when audio is needed.
80** It may called at interrupt level on some machines so don't do anything
81** that could mess up the system like calling malloc() or free().
82*/
83static int patestCallback( void *inputBuffer, void *outputBuffer,
84                           unsigned long framesPerBuffer,
85                           PaTimestamp outTime, void *userData )
86{
87    paTestData *data = (paTestData*)userData;
88    float *out = (float*)outputBuffer;
89    int i;
90    int finished = 0;
91    (void) outTime; /* Prevent unused variable warnings. */
92    (void) inputBuffer;
93
94    for( i=0; i<framesPerBuffer; i++ )
95    {
96        *out++ = LookupSine(data, data->left_phase);  /* left */
97        *out++ = LookupSine(data, data->right_phase);  /* right */
98        data->left_phase += data->phase_increment;
99        if( data->left_phase >= 1.0f ) data->left_phase -= 1.0f;
100        data->right_phase += (data->phase_increment * 1.5f); /* fifth above */
101        if( data->right_phase >= 1.0f ) data->right_phase -= 1.0f;
102    }
103    return 0;
104}
105/*******************************************************************/
106int main(void);
107int main(void)
108{
109    PortAudioStream *stream;
110    PaError err;
111    paTestData data;
112    int i;
113    int done = 0;
114    printf("PortAudio Test: enter letter then hit ENTER. numBuffers = %d\n", NUM_BUFFERS );
115    /* initialise sinusoidal wavetable */
116    for( i=0; i<TABLE_SIZE; i++ )
117    {
118        data.sine[i] = 0.90f * (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
119    }
120    data.sine[TABLE_SIZE] = data.sine[0];       /* set guard point */
121    data.left_phase = data.right_phase = 0.0;
122    data.phase_increment = CalcPhaseIncrement(MIN_FREQ);
123
124    err = Pa_Initialize();
125    if( err != paNoError ) goto error;
126    printf("PortAudio Test: output device = %d\n", OUTPUT_DEVICE );
127    err = Pa_OpenStream(
128              &stream,
129              paNoDevice,
130              0,              /* no input */
131              paFloat32,  /* 32 bit floating point input */
132              NULL,
133              OUTPUT_DEVICE,
134              2,              /* stereo output */
135              paFloat32,      /* 32 bit floating point output */
136              NULL,
137              SAMPLE_RATE,
138              FRAMES_PER_BUFFER,
139              NUM_BUFFERS,    /* number of buffers, if zero then use default minimum */
140              paClipOff|paDitherOff, /* we won't output out of range samples so don't bother clipping them */
141              patestCallback,
142              &data );
143    if( err != paNoError ) goto error;
144    err = Pa_StartStream( stream );
145    if( err != paNoError ) goto error;
146    printf("Play ASCII keyboard. Hit 'q' to stop. (Use RETURN key on Mac)\n");
147    fflush(stdout);
148    while ( !done )
149    {
150        float  freq;
151        int index;
152        char c;
153        do
154        {
155            c = getchar();
156        }
157        while( c < ' '); /* Strip white space and control chars. */
158
159        if( c == 'q' ) done = 1;
160        index = c % 26;
161        freq = MIN_FREQ + (index * 40.0);
162        data.phase_increment = CalcPhaseIncrement(freq);
163    }
164    printf("Call Pa_StopStream()\n");
165    err = Pa_StopStream( stream );
166    if( err != paNoError ) goto error;
167    Pa_Terminate();
168    printf("Test finished.\n");
169    return err;
170error:
171    Pa_Terminate();
172    fprintf( stderr, "An error occured while using the portaudio stream\n" );
173    fprintf( stderr, "Error number: %d\n", err );
174    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
175    return err;
176}
Note: See TracBrowser for help on using the repository browser.