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
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
* aiorecord.cc
*
* Fri Oct 3 09:58:45 CEST 2014
* Copyright 2014 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
/*
* This file is part of LibAudioIO.
*
* LibAudioIO is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* LibAudioIO 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with LibAudioIO; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <getopt.h>
#include <config.h>
#include <signal.h>
#include "device.h"
#include "mixer.h"
static const char version_str[] =
"aiomixer v" VERSION "\n"
;
static const char copyright_str[] =
"Copyright (C) 2014 Bent Bisballe Nyeng - Aasimon.org.\n"
"This is free software. You may redistribute copies of it under the terms of\n"
"the GNU Lesser General Public License "
"<http://www.gnu.org/licenses/lgpl.html>.\n"
"There is NO WARRANTY, to the extent permitted by law.\n"
"\n"
"Written by Bent Bisballe Nyeng (deva@aasimon.org)\n"
;
static const char usage_str[] =
"Usage: %s options\n"
"Options:\n"
" -L, --list List cards.\n"
" -d, --device s Set device to s (default 'default').\n"
" -c, --channels n Set number of channels to n (default 2).\n"
" -s, --samplerate n Set samplerate to n (default 48000).\n"
" -f, --file s Set output file to s (default '/dev/stdout').\n"
" -v, --verbose Print info while working\n"
" -V, --version Print version information and exit.\n"
" -h, --help Print this message and exit.\n"
;
typedef enum {
MODE_UNKNOWN,
MODE_RECORD,
MODE_PLAY,
} aiomode_t;
volatile bool running = true;
void ctrl_c(int)
{
running = false;
}
void vu(const char *pcm, size_t size)
{
static double avg = 0;
short *p = (short*)pcm;
size_t sz = size / sizeof(short);
short max = 0;
for(int i = 0; i < (int)sz; i++) {
if(abs(p[i]) > max) max = abs(p[i]);
}
avg = avg * 0.95 + (double)max * 0.05;
size_t pos = (int)avg / 1000;
if(pos > 32) pos = 32;
if(pos < 0) pos = 0;
char bar[33];
char spc[33];
memset(bar, '#', sizeof(bar));
memset(spc, ' ', sizeof(spc));
bar[pos] = '\0';
spc[sizeof(spc) - pos] = '\0';
printf("\rVU [% 6d]: |%s>%s|", (int)avg, bar, spc);
fflush(stdout);
}
int main(int argc, char *argv[])
{
// This program acts as both a recorder and a player depending on which name
// it is called by.
aiomode_t mode = MODE_UNKNOWN;
char *prog = strrchr(argv[0], '/');
if(prog) prog++; // skip '/'
if(prog == NULL) prog = argv[0];
if(!strcmp(prog, "aiorecord")) mode = MODE_RECORD;
if(!strcmp(prog, "aioplay")) mode = MODE_PLAY;
printf("prog: '%s'\n", prog);
std::string device = "default";
std::string file = "/dev/stdout";
int samplerate = 48000;
int channels = 2;
bool list = false;
bool verbose = false;
int c;
int option_index = 0;
while(1) {
static struct option long_options[] = {
{"list", no_argument, 0, 'L'},
{"device", required_argument, 0, 'd'},
{"channels", required_argument, 0, 'c'},
{"samplerate", required_argument, 0, 's'},
{"file", required_argument, 0, 'f'},
{"verbose", no_argument, 0, 'v'},
{"version", no_argument, 0, 'V'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
c = getopt_long (argc, argv, "hvLd:c:s:f:V",
long_options, &option_index);
if (c == -1)
break;
switch(c) {
case 'L':
list = true;
break;
case 'd':
device = optarg;
break;
case 'c':
channels = atoi(optarg);
break;
case 's':
samplerate = atoi(optarg);
break;
case 'f':
file = optarg;
break;
case 'v':
verbose = true;
break;
case '?':
case 'h':
printf("%s", version_str);
printf(usage_str, argv[0]);
return 0;
case 'V':
printf("%s", version_str);
printf("%s", copyright_str);
return 0;
default:
break;
}
}
if(list) {
// TODO
}
signal(SIGINT, ctrl_c);
Device dev(device);
switch(mode) {
case MODE_RECORD:
{
Source *src = dev.getSource(device, samplerate, channels);
if(!src) {
printf("Could not find source device: %s\n", device.c_str());
}
FILE *fp = fopen(file.c_str(), "w");
if(!fp) {
printf("Could not open %s for writing: ", file.c_str());
perror("");
return 1;
}
size_t buffer_size = src->frames();
char *pcm = (char*)calloc(buffer_size, 1);
while(running) {
int size = src->readSamples(pcm, buffer_size);
if(size < 0) {
printf("PCM read error: %d\n", size);
break;
}
size_t w = fwrite(pcm, size, 1, fp); (void)w;
if(verbose) vu(pcm, size);
}
fclose(fp);
free(pcm);
printf("\ndone\n");
}
break;
case MODE_PLAY:
{
Sink *sink = dev.getSink(device, samplerate, channels);
if(!sink) {
printf("Could not find sink device: %s\n", device.c_str());
}
FILE *fp = fopen(file.c_str(), "r");
if(!fp) {
printf("Could not open %s for reading: ", file.c_str());
perror("");
return 1;
}
size_t buffer_size = sink->frames();
char *pcm = (char*)calloc(buffer_size, 1);
while(running && !feof(fp)) {
size_t size = fread(pcm, 1, buffer_size, fp);
int err = sink->writeSamples(pcm, size);
if(err < 0) {
printf("PCM write error: %d - size: %d\n", err, size);
break;
}
if(verbose) vu(pcm, size);
}
fclose(fp);
free(pcm);
printf("\ndone\n");
}
break;
case MODE_UNKNOWN:
printf("Unknown mode '%s'.\n", prog);
return 1;
}
return 0;
}
|