source: mainline/uspace/app/wavplay/main.c@ 19f6ea5b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 19f6ea5b was 19f6ea5b, checked in by Jan Vesely <jano.vesely@…>, 13 years ago

wavplay: play multiple files

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2012 Jan Vesely
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup wavplay
30 * @{
31 */
32/**
33 * @file PCM playback audio devices
34 */
35
36#include <assert.h>
37#include <errno.h>
38#include <fibril_synch.h>
39#include <str_error.h>
40#include <stdio.h>
41#include <hound/client.h>
42#include <pcm/sample_format.h>
43#include <getopt.h>
44
45#include "dplay.h"
46#include "wave.h"
47
48#define BUFFER_SIZE (32 * 1024)
49
50
51static int hplay(const char *filename)
52{
53 printf("Hound playback: %s\n", filename);
54 FILE *source = fopen(filename, "rb");
55 if (!source) {
56 printf("Failed to open file %s\n", filename);
57 return EINVAL;
58 }
59 wave_header_t header;
60 size_t read = fread(&header, sizeof(header), 1, source);
61 if (read != 1) {
62 printf("Failed to read WAV header: %zu\n", read);
63 fclose(source);
64 return EIO;
65 }
66 pcm_format_t format;
67 const char *error;
68 int ret = wav_parse_header(&header, NULL, NULL, &format.channels,
69 &format.sampling_rate, &format.sample_format, &error);
70 if (ret != EOK) {
71 printf("Error parsing wav header: %s.\n", error);
72 fclose(source);
73 return EINVAL;
74 }
75 hound_context_t *hound = hound_context_create_playback(filename,
76 format, BUFFER_SIZE * 2);
77 if (!hound) {
78 printf("Failed to create HOUND context\n");
79 fclose(source);
80 return ENOMEM;
81 }
82
83 ret = hound_context_connect_target(hound, HOUND_DEFAULT_TARGET);
84 if (ret != EOK) {
85 printf("Failed to connect to default target: %s\n",
86 str_error(ret));
87 hound_context_destroy(hound);
88 fclose(source);
89 return ret;
90 }
91 static char buffer[BUFFER_SIZE];
92 while ((read = fread(buffer, sizeof(char), BUFFER_SIZE, source)) > 0) {
93 ret = hound_write_main_stream(hound, buffer, read);
94 if (ret != EOK) {
95 printf("Failed to write to main context stream: %s\n",
96 str_error(ret));
97 break;
98 }
99 }
100 hound_context_destroy(hound);
101 fclose(source);
102 return ret;
103}
104
105static const struct option opts[] = {
106 {"device", required_argument, 0, 'd'},
107 {"record", no_argument, 0, 'r'},
108 {"help", no_argument, 0, 'h'},
109 {0, 0, 0, 0}
110};
111
112static void print_help(const char* name)
113{
114 printf("Usage: %s [options] file [files...]\n", name);
115 printf("supported options:\n");
116 printf("\t -h, --help\t Print this help.\n");
117 printf("\t -r, --record\t Start recording instead of playback. "
118 "(Not implemented)\n");
119 printf("\t -d, --device\t Use specified device instead of the sound "
120 "service. Use location path or a special device `default'\n");
121}
122
123int main(int argc, char *argv[])
124{
125 const char *device = "default";
126 int idx = 0;
127 bool direct = false, record = false;
128 optind = 0;
129 int ret = 0;
130 while (ret != -1) {
131 ret = getopt_long(argc, argv, "d:rh", opts, &idx);
132 switch (ret) {
133 case 'd':
134 direct = true;
135 device = optarg;
136 break;
137 case 'r':
138 record = true;
139 break;
140 case 'h':
141 print_help(*argv);
142 return 0;
143 };
144 }
145
146 if (optind == argc) {
147 printf("Not enough arguments.\n");
148 print_help(*argv);
149 return 1;
150 }
151
152 for (int i = optind; i < argc; ++i) {
153 const char *file = argv[i];
154
155 printf("%s (%d/%d) %s\n", record ? "Recording" : "Playing",
156 i - optind + 1, argc - optind, file);
157 if (record) {
158 printf("Recording is not supported yet.\n");
159 return 1;
160 }
161 if (direct) {
162 dplay(device, file);
163 } else {
164 hplay(file);
165 }
166 }
167 return 0;
168}
169/**
170 * @}
171 */
Note: See TracBrowser for help on using the repository browser.