To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

The primary repository for this project is hosted at https://github.com/sonic-visualiser/sv-dependency-builds .
This repository is a read-only copy which is updated automatically every hour.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / src / portaudio_20161030_catalina_patch / test / patest_sine_formats.c @ 164:9fa11135915a

History | View | Annotate | Download (6.97 KB)

1
/** @file patest_sine_formats.c
2
        @ingroup test_src
3
        @brief Play a sine wave for several seconds. Test various data formats.
4
        @author Phil Burk
5
*/
6
/*
7
 * $Id$
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
#include <stdio.h>
44
#include <math.h>
45
#include "portaudio.h"
46

    
47
#define NUM_SECONDS        (10)
48
#define SAMPLE_RATE        (44100)
49
#define FRAMES_PER_BUFFER  (512)
50
#define LEFT_FREQ          (SAMPLE_RATE/256.0)  /* So we hit 1.0 */
51
#define RIGHT_FREQ         (500.0)
52
#define AMPLITUDE          (1.0)
53

    
54
/* Select ONE format for testing. */
55
#define TEST_UINT8    (0)
56
#define TEST_INT8     (0)
57
#define TEST_INT16    (1)
58
#define TEST_FLOAT32  (0)
59

    
60
#if TEST_UINT8
61
#define TEST_FORMAT         paUInt8
62
typedef unsigned char       SAMPLE_t;
63
#define SAMPLE_ZERO         (0x80)
64
#define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x)))
65
#define FORMAT_NAME         "Unsigned 8 Bit"
66

    
67
#elif TEST_INT8
68
#define TEST_FORMAT         paInt8
69
typedef char                SAMPLE_t;
70
#define SAMPLE_ZERO         (0)
71
#define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(127.0 * (x)))
72
#define FORMAT_NAME         "Signed 8 Bit"
73

    
74
#elif TEST_INT16
75
#define TEST_FORMAT         paInt16
76
typedef short               SAMPLE_t;
77
#define SAMPLE_ZERO         (0)
78
#define DOUBLE_TO_SAMPLE(x) (SAMPLE_ZERO + (SAMPLE_t)(32767 * (x)))
79
#define FORMAT_NAME         "Signed 16 Bit"
80

    
81
#elif TEST_FLOAT32
82
#define TEST_FORMAT         paFloat32
83
typedef float               SAMPLE_t;
84
#define SAMPLE_ZERO         (0.0)
85
#define DOUBLE_TO_SAMPLE(x) ((SAMPLE_t)(x))
86
#define FORMAT_NAME         "Float 32 Bit"
87
#endif
88

    
89
#ifndef M_PI
90
#define M_PI  (3.14159265)
91
#endif
92

    
93

    
94
typedef struct
95
{
96
    double left_phase;
97
    double right_phase;
98
    unsigned int framesToGo;
99
}
100
paTestData;
101
/* This routine will be called by the PortAudio engine when audio is needed.
102
** It may called at interrupt level on some machines so don't do anything
103
** that could mess up the system like calling malloc() or free().
104
*/
105
static int patestCallback( const void *inputBuffer,
106
                           void *outputBuffer,
107
                           unsigned long framesPerBuffer,
108
                           const PaStreamCallbackTimeInfo* timeInfo,
109
                           PaStreamCallbackFlags statusFlags,
110
                           void *userData )
111
{
112
    paTestData *data = (paTestData*)userData;
113
    SAMPLE_t *out = (SAMPLE_t *)outputBuffer;
114
    int i;
115
    int framesToCalc;
116
    int finished = 0;
117
    (void) inputBuffer; /* Prevent unused variable warnings. */
118

    
119
    if( data->framesToGo < framesPerBuffer )
120
    {
121
        framesToCalc = data->framesToGo;
122
        data->framesToGo = 0;
123
        finished = 1;
124
    }
125
    else
126
    {
127
        framesToCalc = framesPerBuffer;
128
        data->framesToGo -= framesPerBuffer;
129
    }
130

    
131
    for( i=0; i<framesToCalc; i++ )
132
    {
133
        data->left_phase += (LEFT_FREQ / SAMPLE_RATE);
134
        if( data->left_phase > 1.0) data->left_phase -= 1.0;
135
        *out++ = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->left_phase * M_PI * 2. )));
136

    
137
        data->right_phase += (RIGHT_FREQ / SAMPLE_RATE);
138
        if( data->right_phase > 1.0) data->right_phase -= 1.0;
139
        *out++ = DOUBLE_TO_SAMPLE( AMPLITUDE * sin( (data->right_phase * M_PI * 2. )));
140
    }
141
    /* zero remainder of final buffer */
142
    for( ; i<(int)framesPerBuffer; i++ )
143
    {
144
        *out++ = SAMPLE_ZERO; /* left */
145
        *out++ = SAMPLE_ZERO; /* right */
146
    }
147
    return finished;
148
}
149
/*******************************************************************/
150
int main(void);
151
int main(void)
152
{
153
    PaStream *stream;
154
    PaStreamParameters outputParameters;
155
    PaError err;
156
    paTestData data;
157
    int totalSamps;
158

    
159
    printf("PortAudio Test: output " FORMAT_NAME "\n");
160

    
161
    data.left_phase = data.right_phase = 0.0;
162
    data.framesToGo = totalSamps =  NUM_SECONDS * SAMPLE_RATE; /* Play for a few seconds. */
163
    err = Pa_Initialize();
164
    if( err != paNoError ) goto error;
165

    
166

    
167
    outputParameters.device           = Pa_GetDefaultOutputDevice(); /* Default output device. */
168
    outputParameters.channelCount     = 2;                           /* Stereo output */
169
    outputParameters.sampleFormat     = TEST_FORMAT;                 /* Selected above. */
170
    outputParameters.suggestedLatency = Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency;
171
    outputParameters.hostApiSpecificStreamInfo = NULL;
172
    err = Pa_OpenStream( &stream,
173
                         NULL,                  /* No input. */
174
                         &outputParameters,     /* As above. */
175
                         SAMPLE_RATE,
176
                         FRAMES_PER_BUFFER,
177
                         paClipOff,      /* we won't output out of range samples so don't bother clipping them */
178
                         patestCallback,
179
                         &data );
180
    if( err != paNoError ) goto error;
181

    
182
    err = Pa_StartStream( stream );
183
    if( err != paNoError ) goto error;
184

    
185
    printf("Waiting %d seconds for sound to finish.\n", NUM_SECONDS );
186

    
187
    while( ( err = Pa_IsStreamActive( stream ) ) == 1 ) Pa_Sleep(100);
188
    if( err < 0 ) goto error;
189

    
190
    err = Pa_CloseStream( stream );
191
    if( err != paNoError ) goto error;
192
    Pa_Terminate();
193

    
194
    printf("PortAudio Test Finished: " FORMAT_NAME "\n");
195

    
196
    return err;
197
error:
198
    Pa_Terminate();
199
    fprintf( stderr, "An error occured while using the portaudio stream\n" );
200
    fprintf( stderr, "Error number: %d\n", err );
201
    fprintf( stderr, "Error message: %s\n", Pa_GetErrorText( err ) );
202
    return err;
203
}