source: mainline/uspace/app/wavplay/main.c@ 017455e

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

wavplay: Add usage help.

  • Property mode set to 100644
File size: 5.7 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 NAME_MAX 32
49char name[NAME_MAX + 1];
50
51typedef struct {
52 FILE* source;
53 volatile bool playing;
54 fibril_mutex_t mutex;
55 fibril_condvar_t cv;
56 hound_sess_t *server;
57} playback_t;
58
59static void playback_initialize(playback_t *pb, hound_sess_t *sess)
60{
61 assert(pb);
62 pb->playing = false;
63 pb->source = NULL;
64 pb->server = sess;
65 fibril_mutex_initialize(&pb->mutex);
66 fibril_condvar_initialize(&pb->cv);
67}
68
69static void data_callback(void* arg, void *buffer, ssize_t size)
70{
71 playback_t *pb = arg;
72 assert(pb);
73
74 if (size > 0) {
75 const ssize_t bytes =
76 fread(buffer, sizeof(uint8_t), size, pb->source);
77 printf("%zu bytes ready\n", bytes);
78 if (bytes < size) {
79 printf(" requested: %zd ready: %zd zero: %zd\n",
80 size, bytes, size - bytes);
81 bzero(buffer + bytes, size - bytes);
82 }
83 if (bytes == 0) {
84 pb->playing = false;
85 printf("The end, nothing more to play.\n");
86 fibril_condvar_signal(&pb->cv);
87 }
88 } else {
89 printf("Got error %s.\n", str_error(size));
90 pb->playing = false;
91 fibril_condvar_signal(&pb->cv);
92 }
93}
94
95static void play(playback_t *pb, unsigned channels, unsigned rate, pcm_sample_format_t format)
96{
97 assert(pb);
98 /* Create playback client */
99 int ret = hound_register_playback(pb->server, name, channels, rate,
100 format, data_callback, pb);
101 if (ret != EOK) {
102 printf("Failed to register playback: %s\n", str_error(ret));
103 return;
104 }
105
106 /* Connect */
107 ret = hound_create_connection(pb->server, name, DEFAULT_SINK);
108 if (ret == EOK) {
109 fibril_mutex_lock(&pb->mutex);
110 for (pb->playing = true; pb->playing;
111 fibril_condvar_wait(&pb->cv, &pb->mutex));
112 fibril_mutex_unlock(&pb->mutex);
113
114 hound_destroy_connection(pb->server, name, DEFAULT_SINK);
115 } else
116 printf("Failed to connect: %s\n", str_error(ret));
117
118 printf("Unregistering playback\n");
119 hound_unregister_playback(pb->server, name);
120}
121
122static const struct option opts[] = {
123 {"device", required_argument, 0, 'd'},
124 {"record", no_argument, 0, 'r'},
125 {"help", no_argument, 0, 'h'},
126 {0, 0, 0, 0}
127};
128
129static void print_help(const char* name)
130{
131 printf("Usage: %s [options] file\n", name);
132 printf("supported options:\n");
133 printf("\t -h, --help\t Print this help.\n");
134 printf("\t -r, --record\t Start recording instead of playback.\n");
135 printf("\t -d, --device\t Use specified device instead of sound "
136 "service. Use location path or special device `default'\n");
137}
138
139int main(int argc, char *argv[])
140{
141 const char *device = "default";
142 int idx = 0;
143 bool direct = false, record = false;
144 optind = 0;
145 int ret = 0;
146 while (ret != -1) {
147 ret = getopt_long(argc, argv, "d:rh", opts, &idx);
148 switch (ret) {
149 case 'd':
150 direct = true;
151 device = optarg;
152 break;
153 case 'r':
154 record = true;
155 break;
156 case 'h':
157 print_help(*argv);
158 return 0;
159 };
160 }
161
162 if (optind == argc) {
163 printf("Not enough arguments.\n");
164 print_help(*argv);
165 return 1;
166 }
167 const char *file = argv[optind];
168
169 printf("%s %s\n", record ? "Recording" : "Playing", file);
170 if (record) {
171 printf("Recording is not supported yet.\n");
172 return 1;
173 }
174 if (direct)
175 return dplay(device, file);
176
177 task_id_t tid = task_get_id();
178 snprintf(name, NAME_MAX, "%s%" PRIu64 ":%s", argv[0], tid, file);
179
180 printf("Client name: %s\n", name);
181
182 hound_sess_t *sess = hound_get_session();
183 if (!sess) {
184 printf("Failed to connect to hound service\n");
185 return 1;
186 }
187
188 playback_t pb;
189 playback_initialize(&pb, sess);
190 pb.source = fopen(file, "rb");
191 if (pb.source == NULL) {
192 printf("Failed to open %s.\n", file);
193 hound_release_session(sess);
194 return 1;
195 }
196 wave_header_t header;
197 fread(&header, sizeof(header), 1, pb.source);
198 unsigned rate, channels;
199 pcm_sample_format_t format;
200 const char *error;
201 ret = wav_parse_header(&header, NULL, NULL, &channels, &rate,
202 &format, &error);
203 if (ret != EOK) {
204 printf("Error parsing wav header: %s.\n", error);
205 fclose(pb.source);
206 hound_release_session(sess);
207 return 1;
208 }
209
210 play(&pb, channels, rate, format);
211
212 printf("Releasing session\n");
213 hound_release_session(sess);
214 return 0;
215}
216/**
217 * @}
218 */
Note: See TracBrowser for help on using the repository browser.