4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
26 #include <sys/types.h>
31 #include <avsys-audio.h>
33 #define VERSION (0001)
35 void usage(char *name)
37 printf("Usage: %s [OPTION]... [FILE]...\n\n", name);
38 printf("-h\t\thelp\n");
39 printf("-p\t\tplay raw file (default mode 0) \n");
40 printf("-c\t\tcapture audio to raw file (default mode 0)\n");
41 printf("-m\t\tmode\n");
42 printf("\t\tmode 0 : S16LE / Stereo / 44100Hz\n");
43 printf("\t\tmode 1 : S16LE / Mono / 8000Hz\n");
44 printf("\t\tmode 2 : S16LE / Mono / 22050Hz\n");
45 printf("\t\tmode 3 : U8 / Mono / 8000Hz\n");
46 printf("-r\t\trouting\n");
47 printf("\t\trouting 0 : following policy\n");
48 printf("\t\trouting 1 : forced set to alsa\n");
49 printf("-v\t\volume type\n");
50 printf("\t\tvolume type 0 : system\n");
51 printf("\t\tvolume type 1 : notification\n");
52 printf("\t\tvolume type 2 : alarm\n");
53 printf("\t\tvolume type 3 : ringtone\n");
54 printf("\t\tvolume type 4 : media\n");
55 printf("\t\tvolume type 5 : call\n");
69 static struct sigaction sigterm_action; /* Backup pointer of SIGTERM signal handler */
72 int __make_param(int op, int mode, int routing, int voltyp, avsys_audio_param_t *param)
77 if (op == OP_PLAYBACK)
78 param->mode = AVSYS_AUDIO_MODE_OUTPUT;
79 else if (op == OP_CAPTURE)
80 param->mode = AVSYS_AUDIO_MODE_INPUT;
84 param->priority = AVSYS_AUDIO_PRIORITY_NORMAL;
85 param->vol_type = voltyp;
86 param->handle_route = routing;
90 param->format = AVSYS_AUDIO_FORMAT_16BIT;
91 param->samplerate = 44100;
95 param->format = AVSYS_AUDIO_FORMAT_16BIT;
96 param->samplerate = 8000;
100 param->format = AVSYS_AUDIO_FORMAT_16BIT;
101 param->samplerate = 22050;
105 param->format = AVSYS_AUDIO_FORMAT_8BIT;
106 param->samplerate = 8000;
115 int _playback(int mode, int routing, int voltyp, char *filename)
118 avsys_audio_param_t param;
119 avsys_handle_t handle = -1;
120 int recommended_period_size = 0;
128 memset(¶m, '\0', sizeof(avsys_audio_param_t));
130 if (__make_param(OP_PLAYBACK, mode, routing, voltyp, ¶m)) {
131 printf("Can not make audio parameter\n");
135 res = avsys_audio_open(¶m, &handle, &recommended_period_size);
137 printf("Can not open handle 0x%x\n", res);
141 pcmbuf = alloca(recommended_period_size);
145 fd = open(filename, O_RDONLY);
149 while (((size = read(fd, pcmbuf, recommended_period_size)) > 0) && !g_interrupted) {
150 if (AVSYS_FAIL(avsys_audio_write(handle, pcmbuf, recommended_period_size))) {
151 printf("Oops!! audio play fail\n");
157 avsys_audio_close(handle);
161 avsys_audio_close(handle);
169 int _capture(int mode, int routing, int voltyp, char *filename)
172 avsys_audio_param_t param;
173 avsys_handle_t handle = -1;
174 int recommended_period_size = 0;
178 char namebuffer[64]= {0,};
183 snprintf(namebuffer, sizeof(namebuffer)-1, "%s.raw_%1d", filename, mode);
185 printf("[%s] real filename :%s\n", __func__, namebuffer);
187 memset(¶m, '\0', sizeof(avsys_audio_param_t));
189 if (__make_param(OP_CAPTURE, mode, routing, voltyp, ¶m)) {
190 printf("Can not make audio parameter\n");
194 res = avsys_audio_open(¶m, &handle, &recommended_period_size);
196 printf("Can not open handle 0x%x\n", res);
200 pcmbuf = alloca(recommended_period_size);
202 printf("Can not alloca pcm buffer\n");
206 fd = open(namebuffer, O_WRONLY | O_CREAT, 0644);
208 printf("Can not open file %s, %s\n", namebuffer, strerror(errno));
212 while (((size = avsys_audio_read(handle, pcmbuf, recommended_period_size)) > 0) && !g_interrupted) {
213 if (-1 == write(fd, pcmbuf, size))
218 avsys_audio_close(handle);
221 printf("[%s] FAIL\n",__func__);
223 avsys_audio_close(handle);
231 void sig_handler(int signo)
236 int main(int argc, char *argv[])
239 int operation = OP_NONE;
243 int voltyp = 4; /* media */
244 struct sigaction action;
246 while ((opt = getopt(argc, argv, "hpcm:r:v:")) != -1) {
253 if (operation != OP_NONE) {
257 operation = OP_PLAYBACK;
260 if (operation != OP_NONE) {
264 operation = OP_CAPTURE;
268 if (tmp < MODE_MIN || tmp > MODE_MAX) {
270 printf("MESSAGE : Unsupported mode number\n");
277 if (tmp < 0 || tmp > 1) {
279 printf("MESSAGE : Unsupported mode number\n");
286 if (tmp < 0 || tmp > AVSYS_AUDIO_VOLUME_TYPE_NUM) {
288 printf("MESSAGE : invalid volume type\n");
299 action.sa_handler = sig_handler;
301 sigemptyset(&action.sa_mask);
302 sigaction(SIGINT, &action, &sigterm_action);
304 if (operation == OP_NONE) {
306 printf("MESSAGE : Operation is not determined\n");
308 } else if (operation == OP_PLAYBACK) {
310 name = argv[optind++];
311 printf("op %u, mode %u, routing %d, voltyp %d, name %s\n", operation, mode, routing, voltyp, name);
312 _playback(mode, routing, voltyp, name);
313 } else if (operation == OP_CAPTURE) {
315 name = argv[optind++];
316 if (strlen(name) < 2) {
317 printf("Insufficient filename length\n");
320 printf("op %u, mode %u, routing %d, voltyp %d name %s\n", operation, mode, routing, voltyp, name);
321 _capture(mode, routing, voltyp, name);