Repository navigation
Expand file tree
/
Copy pathpulsetest.cpp
More file actions
108 lines (84 loc) · 2.87 KB
/
Copy pathpulsetest.cpp
File metadata and controls
108 lines (84 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "portaudio.h"
#include "PulseWave.hpp"
#include <cstdio>
#include <cstdlib>
#include <string>
// Default: testing with first pitch in Donkey Kong:
// divider 427 (260.747815 Hz), 50% duty
const float TEST_DUTY = 0.5;
const unsigned int TEST_DIVIDER = 427;
const int SAMPLE_RATE = 44100;
const int DURATION = 3;
inline void checkPaError(PaError err) {
if (err != paNoError) {
fprintf(stderr, "PortAudio error: %s\n", Pa_GetErrorText(err));
exit(1);
}
}
typedef struct
{
double t;
float out;
}
paTestData;
static paTestData sdata;
static PulseWave pulse((double) SAMPLE_RATE);
static int patestCallback( const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer,
const PaStreamCallbackTimeInfo* timeInfo,
PaStreamCallbackFlags statusFlags,
void *userData )
{
paTestData *data = (paTestData*)userData;
float *out = (float*)outputBuffer;
unsigned int i;
(void) inputBuffer; /* Prevent unused variable warning. */
for( i=0; i<framesPerBuffer; i++ )
{
*out++ = data->out;
*out++ = data->out;
data->out = pulse.tick() / 15.0;
data->t += ((double)1.0/(double)SAMPLE_RATE);
}
return 0;
}
int main(int argc, char **argv) {
unsigned int divider = TEST_DIVIDER;
if (argc >= 2) {
divider = std::stoi(argv[1]);
}
float duty = TEST_DUTY;
if (argc >= 3) {
duty = std::stof(argv[2]);
}
pulse.setDivider(divider);
pulse.setDuty(duty);
pulse.setEnabled(1);
sdata.t = 0.0;
PaError err = Pa_Initialize();
checkPaError(err);
PaStream *stream;
err = Pa_OpenDefaultStream( &stream,
0, /* no input channels */
2, /* stereo output */
paFloat32, /* 32 bit floating point output */
SAMPLE_RATE,
256, /* frames per buffer, i.e. the number
of sample frames that PortAudio will
request from the callback. Many apps
may want to use
paFramesPerBufferUnspecified, which
tells PortAudio to pick the best,
possibly changing, buffer size.*/
patestCallback, /* this is your callback function */
&sdata ); /*This is a pointer that will be passed to
your callback*/
checkPaError(err);
err = Pa_StartStream( stream );
checkPaError(err);
Pa_Sleep(DURATION*1000);
err = Pa_StopStream( stream );
checkPaError(err);
err = Pa_Terminate();
checkPaError(err);
}