-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
121 lines (109 loc) · 3.46 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
#include "audio.h"
#include <stdio.h>
#include <pulse/error.h>
#include "main.h"
// pactl list | grep -A6 'Sink #'
#define AUX_SEND_DEVICE "alsa_output.usb-C-Media_Electronics_Inc._USB_Audio_Device-00.analog-stereo"
// pactl list | grep -A6 'Source #'
#define RECORD_DEVICE "alsa_input.usb-C-Media_Electronics_Inc._USB_Audio_Device-00.analog-mono"
pa_simple *play_stream;
pa_simple *aux_send_stream;
pa_simple *record_stream;
int audio_open(void) {
static const pa_sample_spec sample_spec = {
.format = PA_SAMPLE_FLOAT32,
.rate = FRAME_RATE,
.channels = OUT_CHANNELS
};
static const pa_buffer_attr playback_buff_attr = {
.tlength = PLAYBACK_BUFFER_SIZE,
.maxlength = -1,
.minreq = -1,
.prebuf = -1
};
static const pa_buffer_attr record_buff_attr = {
.fragsize = RECORD_BUFFER_SIZE,
.maxlength = -1
};
int pa_error;
play_stream = pa_simple_new(
NULL, // default server
"tapedeck", // name of app
PA_STREAM_PLAYBACK,
NULL, // default device
"playback", // stream description
&sample_spec,
NULL, // default channel map
&playback_buff_attr,
&pa_error);
if (!play_stream) {
fprintf(stderr, "Couldn't open playback stream: %s\n", pa_strerror(pa_error));
return 1;
}
aux_send_stream = pa_simple_new(
NULL,
"tapedeck",
PA_STREAM_PLAYBACK,
AUX_SEND_DEVICE,
"aux send",
&sample_spec,
NULL,
&playback_buff_attr,
&pa_error);
if (!aux_send_stream) {
fprintf(stderr, "Couldn't open aux-send stream: %s\n", pa_strerror(pa_error));
return 1;
}
record_stream = pa_simple_new(
NULL, // default server
"tapedeck", // name of app
PA_STREAM_RECORD,
RECORD_DEVICE,
"record", // stream description
&sample_spec,
NULL, // default channel map
&record_buff_attr,
&pa_error);
if (!record_stream) {
fprintf(stderr, "Couldn't open record stream: %s\n", pa_strerror(pa_error));
return 1;
}
return 0;
}
void audio_close(void) {
pa_simple_free(play_stream);
pa_simple_free(aux_send_stream);
pa_simple_free(record_stream);
}
int audio_read(sample * buffer, int num_samples) {
int pa_error;
if (pa_simple_read(record_stream, buffer, num_samples * sizeof(sample), &pa_error) < 0) {
fprintf(stderr, "Read failed: %s\n", pa_strerror(pa_error));
return 1;
}
return 0;
}
int audio_write(sample * buffer, int num_samples) {
int pa_error;
if (pa_simple_write(play_stream, buffer, num_samples * sizeof(sample), &pa_error) < 0) {
fprintf(stderr, "Write failed: %s\n", pa_strerror(pa_error));
return 1;
}
return 0;
}
int audio_write_aux(sample * buffer, int num_samples) {
int pa_error;
if (pa_simple_write(aux_send_stream, buffer, num_samples * sizeof(sample), &pa_error) < 0) {
fprintf(stderr, "Aux write failed: %s\n", pa_strerror(pa_error));
return 1;
}
return 0;
}
int audio_flush(void) {
int pa_error;
if (pa_simple_flush(play_stream, &pa_error) < 0
|| pa_simple_flush(aux_send_stream, &pa_error) < 0
|| pa_simple_flush(record_stream, &pa_error) < 0) {
fprintf(stderr, "Flush failed: %s\n", pa_strerror(pa_error));
}
}