-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsimpleaudio.c
More file actions
184 lines (157 loc) · 4.08 KB
/
Copy pathsimpleaudio.c
File metadata and controls
184 lines (157 loc) · 4.08 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
* simpleaudio.c
*
* Copyright (C) 2011-2012 Kamal Mostafa <kamal@whence.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <jo/jo.h>
#include "saturn-minimodem.h"
#include "simpleaudio.h"
#include "simpleaudio_internal.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
simpleaudio *
simpleaudio_open_stream(
sa_backend_t sa_backend,
const char *backend_device,
sa_direction_t sa_stream_direction,
sa_format_t sa_format,
unsigned int rate, unsigned int channels,
char *app_name, char *stream_name )
{
simpleaudio *sa = jo_malloc(sizeof(simpleaudio));
if ( !sa ) {
jo_core_error("jo_malloc");
return NULL;
}
jo_memset(sa, 0, sizeof(simpleaudio));
sa->format = sa_format;
sa->rate = rate;
sa->channels = channels;
switch ( sa_format ) {
case SA_SAMPLE_FORMAT_FLOAT:
//assert( sizeof(float) == 4 );
sa->samplesize = sizeof(float);
break;
case SA_SAMPLE_FORMAT_S16:
//assert( sizeof(short) == 2 );
sa->samplesize = sizeof(short);
break;
default:
jo_core_error("simpleaudio_open_stream: no such sa_format (%d)\n", sa_format);
goto err_out;
break;
}
switch ( sa_backend ) {
case SA_BACKEND_SEGASATURN:
sa->backend = &simpleaudio_backend_segasaturn;
break;
#if USE_SNDFILE
case SA_BACKEND_FILE:
sa->backend = &simpleaudio_backend_sndfile;
break;
#endif
#if USE_BENCHMARKS
case SA_BACKEND_BENCHMARK:
sa->backend = &simpleaudio_backend_benchmark;
break;
#endif
case SA_BACKEND_SYSDEFAULT:
#if USE_PULSEAUDIO
sa->backend = &simpleaudio_backend_pulseaudio;
#elif USE_ALSA
sa->backend = &simpleaudio_backend_alsa;
#else
jo_core_error("simpleaudio_open_stream: no SA_BACKEND_SYSDEFAULT was configured\n");
goto err_out;
#endif
break;
#if USE_ALSA
case SA_BACKEND_ALSA:
sa->backend = &simpleaudio_backend_alsa;
break;
#endif
#if USE_PULSEAUDIO
case SA_BACKEND_PULSEAUDIO:
sa->backend = &simpleaudio_backend_pulseaudio;
break;
#endif
default:
jo_core_error("simpleaudio_open_stream: no such sa_backend (%d). not configured at build?\n", sa_backend);
goto err_out;
}
int ok = sa->backend->simpleaudio_open_stream(sa,
backend_device, sa_stream_direction, sa_format,
rate, channels, app_name, stream_name);
if ( sa->channels != channels ) {
jo_core_error("%s: input stream must be %u-channel (not %u)\n",
stream_name, channels, sa->channels);
simpleaudio_close(sa);
return 0;
}
if ( ok ) {
return sa;
}
err_out:
jo_free(sa);
return NULL;
}
sa_format_t
simpleaudio_get_format( simpleaudio *sa )
{
return sa->format;
}
unsigned int
simpleaudio_get_rate( simpleaudio *sa )
{
return sa->rate;
}
unsigned int
simpleaudio_get_channels( simpleaudio *sa )
{
return sa->channels;
}
unsigned int
simpleaudio_get_framesize( simpleaudio *sa )
{
return sa->backend_framesize;
}
unsigned int
simpleaudio_get_samplesize( simpleaudio *sa )
{
return sa->samplesize;
}
void
simpleaudio_set_rxnoise( simpleaudio *sa, float rxnoise_factor )
{
sa->rxnoise = rxnoise_factor;
}
ssize_t
simpleaudio_read( simpleaudio *sa, void *buf, size_t nframes )
{
return sa->backend->simpleaudio_read(sa, buf, nframes);
}
ssize_t
simpleaudio_write( simpleaudio *sa, void *buf, size_t nframes )
{
return sa->backend->simpleaudio_write(sa, buf, nframes);
}
void
simpleaudio_close( simpleaudio *sa )
{
sa->backend->simpleaudio_close(sa);
jo_free(sa);
}