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

« back to all changes in this revision

Viewing changes to lib-src/portaudio-v19/test/debug_multi_out.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
 
/*
2
 
 * $Id: debug_multi_out.c,v 1.2 2006/09/23 18:42:51 llucius Exp $
3
 
 * debug_multi_out.c
4
 
 * Play a different sine wave on each channels,
5
 
 * using the Portable Audio api.
6
 
 *
7
 
 * Author: Phil Burk  http://www.softsynth.com
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 OUTPUT_DEVICE       (Pa_GetDefaultOutputDeviceID())
49
 
#define SAMPLE_RATE         (44100)
50
 
#define FRAMES_PER_BUFFER   (256)
51
 
#define FREQ_INCR           (300.0 / SAMPLE_RATE)
52
 
#define MAX_CHANNELS        (64)
53
 
 
54
 
#ifndef M_PI
55
 
#define M_PI  (3.14159265)
56
 
#endif
57
 
 
58
 
typedef struct
59
 
{
60
 
    int      numChannels;
61
 
    double   phases[MAX_CHANNELS];
62
 
}
63
 
paTestData;
64
 
 
65
 
/* This routine will be called by the PortAudio engine when audio is needed.
66
 
** It may called at interrupt level on some machines so don't do anything
67
 
** that could mess up the system like calling malloc() or free().
68
 
*/
69
 
static int patestCallback( void *inputBuffer, void *outputBuffer,
70
 
                           unsigned long framesPerBuffer,
71
 
                           PaTimestamp outTime, void *userData )
72
 
{
73
 
    paTestData *data = (paTestData*)userData;
74
 
    float *out = (float*)outputBuffer;
75
 
    int frameIndex, channelIndex;
76
 
    int finished = 0;
77
 
    (void) outTime; /* Prevent unused variable warnings. */
78
 
    (void) inputBuffer;
79
 
 
80
 
    for( frameIndex=0; frameIndex<(int)framesPerBuffer; frameIndex++ )
81
 
    {
82
 
        for( channelIndex=0; channelIndex<data->numChannels; channelIndex++ )
83
 
        {
84
 
            /* Output sine wave on every channel. */
85
 
            *out++ = (float) sin(data->phases[channelIndex]);
86
 
 
87
 
            /* Play each channel at a higher frequency. */
88
 
            data->phases[channelIndex] += FREQ_INCR * (4 + channelIndex);
89
 
            if( data->phases[channelIndex] >= (2.0 * M_PI) ) data->phases[channelIndex] -= (2.0 * M_PI);
90
 
        }
91
 
    }
92
 
 
93
 
    return 0;
94
 
}
95
 
/*******************************************************************/
96
 
int main(void);
97
 
int main(void)
98
 
{
99
 
    PortAudioStream *stream;
100
 
    PaError err;
101
 
    const PaDeviceInfo *pdi;
102
 
    paTestData data = {0};
103
 
    printf("PortAudio Test: output sine wave on each channel.\n" );
104
 
 
105
 
    err = Pa_Initialize();
106
 
    if( err != paNoError ) goto error;
107
 
 
108
 
    pdi = Pa_GetDeviceInfo( OUTPUT_DEVICE );
109
 
    data.numChannels = pdi->maxOutputChannels;
110
 
    if( data.numChannels > MAX_CHANNELS ) data.numChannels = MAX_CHANNELS;
111
 
    printf("Number of Channels = %d\n", data.numChannels );
112
 
    
113
 
    err = Pa_OpenStream(
114
 
              &stream,
115
 
              paNoDevice, /* default input device */
116
 
              0,              /* no input */
117
 
              paFloat32,  /* 32 bit floating point input */
118
 
              NULL,
119
 
              OUTPUT_DEVICE,
120
 
              data.numChannels,
121
 
              paFloat32,  /* 32 bit floating point output */
122
 
              NULL,
123
 
              SAMPLE_RATE,
124
 
              FRAMES_PER_BUFFER,  /* frames per buffer */
125
 
              0,    /* number of buffers, if zero then use default minimum */
126
 
              paClipOff,      /* we won't output out of range samples so don't bother clipping them */
127
 
              patestCallback,
128
 
              &data );
129
 
    if( err != paNoError ) goto error;
130
 
 
131
 
    err = Pa_StartStream( stream );
132
 
    if( err != paNoError ) goto error;
133
 
 
134
 
    printf("Hit ENTER to stop sound.\n");
135
 
    fflush(stdout);
136
 
    getchar();
137
 
 
138
 
    err = Pa_StopStream( stream );
139
 
    if( err != paNoError ) goto error;
140
 
 
141
 
    Pa_CloseStream( stream );
142
 
    Pa_Terminate();
143
 
    printf("Test finished.\n");
144
 
    return err;
145
 
error:
146
 
    Pa_Terminate();
147
 
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
148
 
    fprintf( stderr, "Error number: %d\n", err );
149
 
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
150
 
    return err;
151
 
}