-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
portaudio.c
180 lines (157 loc) · 5.65 KB
/
portaudio.c
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
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "portaudio.h"
#include "common.h"
#include <portaudio.h>
#define SAMPLE_SILENCE -32767
#define PA_SAMPLE_TYPE paInt16
typedef short SAMPLE;
typedef struct {
int frameIndex; /* Index into sample array. */
int maxFrameIndex;
SAMPLE *recordedSamples;
} paTestData;
static struct audio_data *audio;
int16_t silence_buffer[8092] = {SAMPLE_SILENCE};
static int recordCallback(const void *inputBuffer, void *outputBuffer,
unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
PaStreamCallbackFlags statusFlags, void *userData) {
paTestData *data = (paTestData *)userData;
SAMPLE *rptr = (SAMPLE *)inputBuffer;
long framesToCalc;
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)
write_to_input_buffers(framesToCalc * 2, silence_buffer, audio);
else
write_to_input_buffers(framesToCalc * 2, rptr, audio);
data->frameIndex += framesToCalc;
if (finished == paComplete) {
data->frameIndex = 0;
finished = paContinue;
}
if (audio->terminate == 1)
finished = paComplete;
return finished;
}
void portaudio_simple_free(paTestData data) {
Pa_Terminate();
free(data.recordedSamples);
}
void *input_portaudio(void *audiodata) {
audio = (struct audio_data *)audiodata;
PaStreamParameters inputParameters;
PaStream *stream;
PaError err = paNoError;
paTestData data;
// start portaudio
err = Pa_Initialize();
if (err != paNoError) {
fprintf(stderr, "Error: unable to initilize portaudio - %s\n", Pa_GetErrorText(err));
exit(EXIT_FAILURE);
}
// get portaudio device
int deviceNum = -1, numOfDevices = Pa_GetDeviceCount();
if (!strcmp(audio->source, "list")) {
if (numOfDevices < 0) {
fprintf(stderr, "Error: portaudio was unable to find a audio device! Code: 0x%x\n",
numOfDevices);
exit(EXIT_FAILURE);
}
for (int i = 0; i < numOfDevices; i++) {
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(i);
printf("Device #%d: %s\n"
"\tInput Channels: %d\n"
"\tOutput Channels: %d\n"
"\tDefault SampleRate: %lf\n",
i + 1, deviceInfo->name, deviceInfo->maxInputChannels,
deviceInfo->maxOutputChannels, deviceInfo->defaultSampleRate);
}
exit(EXIT_SUCCESS);
} else if (!strcmp(audio->source, "auto")) {
deviceNum = Pa_GetDefaultInputDevice();
if (deviceNum == paNoDevice) {
fprintf(stderr, "Error: no portaudio input device found\n");
exit(EXIT_FAILURE);
}
} else if (sscanf(audio->source, "%d", &deviceNum)) {
if (deviceNum > numOfDevices) {
fprintf(stderr, "Error: Invalid input device!\n");
exit(EXIT_FAILURE);
}
deviceNum--;
} else {
for (int i = 0; i < numOfDevices; i++) {
const PaDeviceInfo *deviceInfo = Pa_GetDeviceInfo(i);
if (!strcmp(audio->source, deviceInfo->name)) {
deviceNum = i;
break;
}
}
if (deviceNum == -1) {
fprintf(stderr, "Error: No such device '%s'!\n", audio->source);
exit(EXIT_FAILURE);
}
}
inputParameters.device = deviceNum;
// set parameters
data.maxFrameIndex = audio->input_buffer_size * 1024 / 2;
data.recordedSamples = (SAMPLE *)malloc(2 * data.maxFrameIndex * sizeof(SAMPLE));
if (data.recordedSamples == NULL) {
fprintf(stderr, "Error: failure in memory allocation!\n");
exit(EXIT_FAILURE);
} else
memset(data.recordedSamples, 0x00, 2 * data.maxFrameIndex);
inputParameters.channelCount = 2;
inputParameters.sampleFormat = PA_SAMPLE_TYPE;
inputParameters.suggestedLatency =
Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency;
inputParameters.hostApiSpecificStreamInfo = NULL;
// set it to work
err = Pa_OpenStream(&stream, &inputParameters, NULL, audio->rate, audio->input_buffer_size / 2,
paClipOff, recordCallback, &data);
if (err != paNoError) {
fprintf(stderr, "Error: failure in opening stream (%s)\n", Pa_GetErrorText(err));
exit(EXIT_FAILURE);
}
// main loop
while (1) {
// start recording
data.frameIndex = 0;
err = Pa_StartStream(stream);
if (err != paNoError) {
fprintf(stderr, "Error: failure in starting stream (%s)\n", Pa_GetErrorText(err));
exit(EXIT_FAILURE);
}
// record
while ((err = Pa_IsStreamActive(stream)) == 1) {
Pa_Sleep(1000);
if (audio->terminate == 1)
break;
}
// check for errors
if (err < 0) {
fprintf(stderr, "Error: failure in recording audio (%s)\n", Pa_GetErrorText(err));
exit(EXIT_FAILURE);
}
// check if it bailed
if (audio->terminate == 1)
break;
}
// close stream
if ((err = Pa_CloseStream(stream)) != paNoError) {
fprintf(stderr, "Error: failure in closing stream (%s)\n", Pa_GetErrorText(err));
exit(EXIT_FAILURE);
}
portaudio_simple_free(data);
return 0;
}