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_longsine.c @ 164:9fa11135915a

History | View | Annotate | Download (5.37 KB)

1
/** @file patest_longsine.c
2
        @ingroup test_src
3
        @brief Play a sine wave until ENTER hit.
4
        @author Phil Burk  http://www.softsynth.com
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
 
44
#include <stdio.h>
45
#include <math.h>
46

    
47
#include "portaudio.h"
48

    
49
#define SAMPLE_RATE   (44100)
50

    
51
#ifndef M_PI
52
#define M_PI  (3.14159265)
53
#endif
54

    
55
#define TABLE_SIZE   (200)
56
typedef struct
57
{
58
    float sine[TABLE_SIZE];
59
    int left_phase;
60
    int right_phase;
61
}
62
paTestData;
63

    
64
/* This routine will be called by the PortAudio engine when audio is needed.
65
** It may called at interrupt level on some machines so don't do anything
66
** that could mess up the system like calling malloc() or free().
67
*/
68
static int patestCallback(const void*                     inputBuffer,
69
                          void*                           outputBuffer,
70
                          unsigned long                   framesPerBuffer,
71
                          const PaStreamCallbackTimeInfo* timeInfo,
72
                          PaStreamCallbackFlags           statusFlags,
73
                          void*                           userData)
74
{
75
    paTestData *data = (paTestData*)userData;
76
    float *out = (float*)outputBuffer;
77
    unsigned int i;
78
    (void) inputBuffer; /* Prevent unused argument warning. */
79
    for( i=0; i<framesPerBuffer; i++ )
80
    {
81
        *out++ = data->sine[data->left_phase];  /* left */
82
        *out++ = data->sine[data->right_phase];  /* right */
83
        data->left_phase += 1;
84
        if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE;
85
        data->right_phase += 3; /* higher pitch so we can distinguish left and right. */
86
        if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE;
87
    }
88
    return 0;
89
}
90

    
91
/*******************************************************************/
92
int main(void);
93
int main(void)
94
{
95
    PaStreamParameters outputParameters;
96
    PaStream *stream;
97
    PaError err;
98
    paTestData data;
99
    int i;
100
    printf("PortAudio Test: output sine wave.\n");
101

    
102
    /* initialise sinusoidal wavetable */
103
    for( i=0; i<TABLE_SIZE; i++ )
104
    {
105
        data.sine[i] = (float) sin( ((double)i/(double)TABLE_SIZE) * M_PI * 2. );
106
    }
107
    data.left_phase = data.right_phase = 0;
108

    
109
    err = Pa_Initialize();
110
    if( err != paNoError ) goto error;
111

    
112
    outputParameters.device = Pa_GetDefaultOutputDevice(); /* default output device */
113
    if (outputParameters.device == paNoDevice) {
114
      fprintf(stderr,"Error: No default output device.\n");
115
      goto error;
116
    }
117
    outputParameters.channelCount = 2;                     /* stereo output */
118
    outputParameters.sampleFormat = paFloat32;             /* 32 bit floating point output */
119
    outputParameters.suggestedLatency = Pa_GetDeviceInfo( outputParameters.device )->defaultLowOutputLatency;
120
    outputParameters.hostApiSpecificStreamInfo = NULL;
121

    
122
    err = Pa_OpenStream( &stream,
123
                         NULL,              /* No input. */
124
                         &outputParameters, /* As above. */
125
                         SAMPLE_RATE,
126
                         256,               /* Frames per buffer. */
127
                         paClipOff,         /* No out of range samples expected. */
128
                         patestCallback,
129
                         &data );
130
    if( err != paNoError ) goto error;
131

    
132
    err = Pa_StartStream( stream );
133
    if( err != paNoError ) goto error;
134

    
135
    printf("Hit ENTER to stop program.\n");
136
    getchar();
137

    
138
    err = Pa_CloseStream( stream );
139
    if( err != paNoError ) goto error;
140
    Pa_Terminate();
141

    
142
    printf("Test finished.\n");
143
    return err;
144

    
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
}