Repository navigation
Expand file tree
/
Copy pathPortaudioRecording.cpp
More file actions
167 lines (140 loc) · 4.66 KB
/
Copy pathPortaudioRecording.cpp
File metadata and controls
167 lines (140 loc) · 4.66 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using namespace std;
#include <portaudio.h>
#include "AnalyserDefinitions.h"
#include <string>
typedef struct
{
int frameIndex; /* Index into sample array. */
int maxFrameIndex;
SAMPLE *recordedSamples;
} paTestData;
PaStreamParameters inputParameters, outputParameters;
PaStream *stream;
PaError err = paNoError;
paTestData data;
//forward Declarations
void cleanupPAAndExit(string message);
void cleanupPA();
void cleanupPA(string message);
static int recordCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData);
SAMPLE *retrieveResults()
{
while ((err = Pa_IsStreamActive(stream)) == 1)
Pa_Sleep(100);
if (err < 0)
cleanupPAAndExit("Error while checking if the stream is active!");
err = Pa_CloseStream(stream);
if (err != paNoError)
cleanupPAAndExit("Error with closing the stream!");
return data.recordedSamples;
}
void resetPA()
{
data.frameIndex = 0;
fill(data.recordedSamples, data.recordedSamples + numSamples, 0);
}
void startRecording()
{
resetPA();
// SAMPLE max, val;
err = Pa_OpenStream(&stream, &inputParameters, NULL, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, recordCallback, &data);
if (err != paNoError)
cleanupPAAndExit("");
err = Pa_StartStream(stream);
if (err != paNoError)
cleanupPAAndExit("");
}
void initializePA()
{
data.maxFrameIndex = totalFrames;
data.recordedSamples = (SAMPLE *)malloc(numSamples * sizeof(SAMPLE)); /* From now on, recordedSamples is initialised. */
if (data.recordedSamples == NULL)
cleanupPAAndExit("Could not allocate record array.\n");
// re route stderr to hide ALSA errors that cannot easily be disabled
// see: https://stackoverflow.com/questions/24778998/how-to-disable-or-re-route-alsa-lib-logging
freopen("/dev/null", "w", stderr);
err = Pa_Initialize();
freopen("/dev/tty", "w", stderr);
if (err != paNoError)
cleanupPAAndExit("");
inputParameters.device = Pa_GetDefaultInputDevice(); /* default input device */
if (inputParameters.device == paNoDevice)
cleanupPAAndExit("Error: No default input device.\n");
inputParameters.channelCount = NUM_CHANNELS; /* stereo input */
inputParameters.sampleFormat = PA_SAMPLE_TYPE;
inputParameters.suggestedLatency = Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
data.frameIndex = 0;
}
void cleanupPAAndExit(string message)
{
cleanupPA(message);
exit(1);
}
void cleanupPA()
{
cleanupPA("");
}
void cleanupPA(string message)
{
if (message != "")
printf(message.c_str());
Pa_Terminate();
if (data.recordedSamples)
free(data.recordedSamples);
if (err != paNoError)
{
fprintf(stderr, "An error occured while using the portaudio stream\n");
fprintf(stderr, "Error number: %d\n", err);
fprintf(stderr, "Error message: %s\n", Pa_GetErrorText(err));
err = 1; /* Always return 0 or 1, but no other return codes. */
}
}
/* This routine will be called by the PortAudio engine when audio is needed.
** It may be called at interrupt level on some machines so don't do anything
** that could mess up the system like calling malloc() or free().
*/
static int recordCallback(const void *inputBuffer, void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo, PaStreamCallbackFlags statusFlags, void *userData)
{
paTestData *data = (paTestData *)userData;
const SAMPLE *rptr = (const SAMPLE *)inputBuffer;
SAMPLE *wptr = &data->recordedSamples[data->frameIndex * NUM_CHANNELS];
long framesToCalc;
long i;
int finished;
unsigned long framesLeft = data->maxFrameIndex - data->frameIndex;
(void)outputBuffer; /* Prevent unused variable warnings. */
(void)timeInfo;
(void)statusFlags;
(void)userData;
if (framesLeft < framesPerBuffer)
{
framesToCalc = framesLeft;
finished = paComplete;
}
else
{
framesToCalc = framesPerBuffer;
finished = paContinue;
}
if (inputBuffer == NULL)
{
for (i = 0; i < framesToCalc; i++)
{
*wptr++ = SAMPLE_SILENCE; /* left */
if (NUM_CHANNELS == 2)
*wptr++ = SAMPLE_SILENCE; /* right */
}
}
else
{
for (i = 0; i < framesToCalc; i++)
{
*wptr++ = *rptr++; /* left */
if (NUM_CHANNELS == 2)
*wptr++ = *rptr++; /* right */
}
}
data->frameIndex += framesToCalc;
return finished;
}