-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
283 lines (235 loc) · 6.15 KB
/
audio.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "audio.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <alsa/asoundlib.h>
#define FRAMES_IN_BLOCK 0x1000
#define BLOCKSIZE (FRAMES_IN_BLOCK * (WAVE_BITS_PER_SAMPLE / 8))
#define PCM_FORMAT SND_PCM_FORMAT_S16_LE
struct block
{
void *buf;
struct block *next;
};
struct block *head = NULL;
snd_pcm_t *handle = NULL;
int finish = 2;
unsigned int blocknumber = 0;
void capture(void);
int capture_start(const char *device)
{
unsigned int freq = WAVE_SAMPLE_RATE;
int freq_adjust_direction = 1;
snd_pcm_hw_params_t *hw_params = NULL;
pthread_t t;
if (snd_pcm_open(&handle, !device ? "default" : device, SND_PCM_STREAM_CAPTURE, 0) < 0)
{
fprintf(stderr, "Could device %s don't open\n", !device ? "default" : device);
return -1;
}
if (snd_pcm_hw_params_malloc(&hw_params) < 0)
{
fprintf(stderr, "Error reserving memory for hardware parameters\n");
return -2;
}
if (snd_pcm_hw_params_any(handle, hw_params) < 0)
{
fprintf(stderr, "Error getting the Standard-Hardware-Parameter\n");
return -3;
}
if (snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
{
fprintf(stderr, "Error setting read\\write access\n");
return -4;
}
if (snd_pcm_hw_params_set_format(handle, hw_params, PCM_FORMAT) < 0)
{
fprintf(stderr, "Error setting the PCM format\n");
return -5;
}
if (snd_pcm_hw_params_set_rate_near(handle, hw_params, &freq, &freq_adjust_direction) < 0)
{
fprintf(stderr, "Error setting the Frequenz\n");
return -6;
}
if (snd_pcm_hw_params_set_channels(handle, hw_params, WAVE_CHANNELS) < 0)
{
fprintf(stderr, "Error setting the number of channels\n");
return -7;
}
if (snd_pcm_hw_params(handle, hw_params) < 0)
{
fprintf(stderr, "Error setting the Hardwareparameter\n");
return -8;
}
snd_pcm_hw_params_free(hw_params);
if (snd_pcm_prepare(handle) < 0)
{
fprintf(stderr, "Error setting the Handles\n");
return -9;
}
finish = 0;
if (pthread_create(&t, NULL, (void *)capture, NULL) < 0)
{
fprintf(stderr, "Error starting recording thread\n");
return -10;
}
return 0;
}
void capture_stop(unsigned int *size, void **buffer)
{
unsigned int i = 0;
struct block *now = NULL;
void **adresses = NULL;
finish = 1;
while (finish != 2);
snd_pcm_close(handle);
*buffer = malloc(BLOCKSIZE * blocknumber);
*size = BLOCKSIZE * blocknumber;
adresses = malloc(blocknumber * sizeof(void *));
now = head;
for (i = 0; i < blocknumber; i++)
{
now = now->next;
adresses[i] = now;
memcpy(*buffer + (i * BLOCKSIZE), now->buf, BLOCKSIZE);
free(now->buf);
}
for (i = 0; i < blocknumber; i++)
free(adresses[i]);
free(head);
free(adresses);
}
int play(const char *device, const void *buffer, const unsigned int size)
{
int r = 0;
unsigned int freq = WAVE_SAMPLE_RATE;
unsigned int framenumber = size / (WAVE_BITS_PER_SAMPLE / 8);
snd_pcm_hw_params_t *hw_params = NULL;
if (snd_pcm_open(&handle, !device ? "default" : device, SND_PCM_STREAM_PLAYBACK, 0) < 0)
{
fprintf(stderr, "Konnte Geraet %s nicht oeffnen\n", !device ? "default" : device);
return -1;
}
if (snd_pcm_hw_params_malloc(&hw_params) < 0)
{
fprintf(stderr, "Error reserving memory for hardware parameters\n");
return -2;
}
if (snd_pcm_hw_params_any(handle, hw_params) < 0)
{
fprintf(stderr, "Error getting default hardware parameters\n");
return -3;
}
//Setting the hardware parameters within the structure
if (snd_pcm_hw_params_set_access(handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED) < 0)
{
fprintf(stderr, "Error setting read\\write access\n");
return -4;
}
if (snd_pcm_hw_params_set_format(handle, hw_params, PCM_FORMAT) < 0)
{
fprintf(stderr, "Error setting the PCM-Formates\n");
return -5;
}
if (snd_pcm_hw_params_set_rate_near(handle, hw_params, &freq, 0) < 0)
{
fprintf(stderr, "Error setting the Frequenz\n");
return -6;
}
if (snd_pcm_hw_params_set_channels(handle, hw_params, WAVE_CHANNELS) < 0)
{
fprintf(stderr, "Error setting the number of channels\n");
return -7;
}
{
fprintf(stderr, "Error setting the Hardwareparameter\n");
return -8;
}
snd_pcm_hw_params_free(hw_params);
//Setting the Hardware-Parameter Handle
if (snd_pcm_prepare(handle) < 0)
{
fprintf(stderr, "Error setting the number of Handles\n");
return -9;
}
//Playing the data in the buffer
do
{
r = snd_pcm_writei(handle, buffer + (size - framenumber * (WAVE_BITS_PER_SAMPLE / 8)), framenumber);
if (r < 0)
fprintf(stderr, "WARNING: Error writing Audio\n");
else
framenumber -= r;
} while (framenumber > 0);
snd_pcm_close(handle);
return 0;
}
void get_device_list(char ****devices, unsigned int *number)
{
unsigned int i = 0;
void **hints = NULL;
//Hole die Liste der PCM-Geraete
snd_device_name_hint(-1, "pcm", &hints);
//Anzahl der Geraete ermitteln
while (hints[i])
i++;
*number = i;
//Speicher reservieren
*devices = malloc(sizeof(char **) * i);
//Eigenschaften der einzelnen Geraete ermitteln
i = 0;
while (hints[i])
{
char *ioid = NULL;
(*devices)[i] = malloc(sizeof(char *) * 3);
//Name holen
(*devices)[i][0] = snd_device_name_get_hint(hints[i], "NAME");
//Beschreibung holen
(*devices)[i][1] = snd_device_name_get_hint(hints[i], "DESC");
//Typ holen
ioid = snd_device_name_get_hint(hints[i], "IOID");
//Input oder Output
if (ioid)
(*devices)[i][2] = ioid;
//Beides
else
{
(*devices)[i][2] = malloc(sizeof(char) * 7);
strncpy((*devices)[i][2], "Beides\0", 7);
}
i++;
}
//Speicher freigeben
snd_device_name_free_hint(hints);
}
void capture(void)
{
struct block *now = NULL;
int r;
//Initialisieren
blocknumber = 0;
head = malloc(sizeof(struct block));
head->buf = NULL;
head->next = NULL;
now = head;
while (!finish)
{
unsigned int framenumber = FRAMES_IN_BLOCK;
blocknumber++;
now->next = malloc(sizeof(struct block));
now = now->next;
now->buf = malloc(BLOCKSIZE);
now->next = NULL;
do
{
r = snd_pcm_readi(handle, now->buf + (FRAMES_IN_BLOCK - framenumber) * (WAVE_BITS_PER_SAMPLE / 8), framenumber);
if (r < 0)
fprintf(stderr, "WARNING: Fehler beim Lesen von Audio\n");
else
framenumber -= r;
} while (framenumber > 0);
}
finish = 2;
}