source: mainline/uspace/app/dplay/dplay.c@ 9d5244f

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

app/dplay: Use event interface properly.

  • Property mode set to 100644
File size: 6.7 KB
Line 
1/*
2 * Copyright (c) 2011 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 dplay
30 * @{
31 */
32/**
33 * @file PCM playback audio devices
34 */
35
36#include <assert.h>
37#include <errno.h>
38#include <str_error.h>
39#include <str.h>
40#include <devman.h>
41#include <audio_pcm_buffer_iface.h>
42#include <fibril_synch.h>
43#include <stdio.h>
44#include <sys/mman.h>
45#include <sys/time.h>
46
47#include <stdio.h>
48#include <macros.h>
49
50#include "wave.h"
51
52#define DEFAULT_DEVICE "/hw/pci0/00:01.0/sb16/dsp"
53#define SUBBUFFERS 2
54
55typedef struct {
56 struct {
57 void *base;
58 size_t size;
59 unsigned id;
60 void* position;
61 } buffer;
62 FILE* source;
63 volatile bool playing;
64 fibril_mutex_t mutex;
65 fibril_condvar_t cv;
66 async_exch_t *device;
67} playback_t;
68
69static void playback_initialize(playback_t *pb, async_exch_t *exch)
70{
71 assert(exch);
72 assert(pb);
73 pb->buffer.id = 0;
74 pb->buffer.base = NULL;
75 pb->buffer.size = 0;
76 pb->playing = false;
77 pb->source = NULL;
78 pb->device = exch;
79 fibril_mutex_initialize(&pb->mutex);
80 fibril_condvar_initialize(&pb->cv);
81}
82
83
84static void device_event_callback(ipc_callid_t iid, ipc_call_t *icall, void* arg)
85{
86 static unsigned wait = SUBBUFFERS;
87 while (1) {
88 ipc_call_t call;
89 ipc_callid_t callid = async_get_call(&call);
90 if (IPC_GET_IMETHOD(call) != IPC_FIRST_USER_METHOD) {
91 printf("Unknown event.\n");
92 async_answer_0(callid,EOK);
93 break;
94 }
95 playback_t *pb = arg;
96// printf("Got device event!!!\n");
97 const size_t buffer_part = pb->buffer.size / SUBBUFFERS;
98 const size_t bytes = fread(pb->buffer.position, sizeof(uint8_t),
99 buffer_part, pb->source);
100 pb->buffer.position += bytes;
101 if (bytes != buffer_part)
102 bzero(pb->buffer.position, buffer_part - bytes);
103 pb->buffer.position += buffer_part - bytes;
104 if (pb->buffer.position >= (pb->buffer.base + pb->buffer.size))
105 pb->buffer.position = pb->buffer.base;
106 async_answer_0(callid,EOK);
107 if (bytes == 0 && (wait-- == 0)) {
108 pb->playing = false;
109 fibril_condvar_signal(&pb->cv);
110 return;
111 }
112 }
113}
114
115
116static void play(playback_t *pb, unsigned sampling_rate, unsigned sample_size,
117 unsigned channels, bool sign)
118{
119 assert(pb);
120 assert(pb->device);
121 pb->buffer.position = pb->buffer.base;
122 printf("Playing: %dHz, %d-bit samples, %d channel(s), %sSIGNED.\n",
123 sampling_rate, sample_size, channels, sign ? "": "UN");
124 const size_t bytes = fread(pb->buffer.base, sizeof(uint8_t),
125 pb->buffer.size, pb->source);
126 if (bytes != pb->buffer.size)
127 return;
128 printf("Buffer data ready.\n");
129 fibril_mutex_lock(&pb->mutex);
130 int ret = audio_pcm_buffer_start_playback(pb->device, pb->buffer.id,
131 SUBBUFFERS, sampling_rate, sample_size, channels, sign);
132 if (ret != EOK) {
133 fibril_mutex_unlock(&pb->mutex);
134 printf("Failed to start playback: %s.\n", str_error(ret));
135 return;
136 }
137
138 for (pb->playing = true; pb->playing;
139 fibril_condvar_wait(&pb->cv, &pb->mutex));
140
141 audio_pcm_buffer_stop_playback(pb->device, pb->buffer.id);
142}
143
144int main(int argc, char *argv[])
145{
146 const char *device = DEFAULT_DEVICE;
147 const char *file;
148 switch (argc) {
149 case 2:
150 file = argv[1];
151 break;
152 case 3:
153 device = argv[1];
154 file = argv[2];
155 break;
156 default:
157 printf("Usage: %s [device] file.\n", argv[0]);
158 return 1;
159 }
160
161 devman_handle_t pcm_handle;
162 int ret = devman_fun_get_handle(device, &pcm_handle, 0);
163 if (ret != EOK) {
164 printf("Failed to get device(%s) handle: %s.\n",
165 device, str_error(ret));
166 return 1;
167 }
168
169 async_sess_t *session = devman_device_connect(
170 EXCHANGE_SERIALIZE, pcm_handle, IPC_FLAG_BLOCKING);
171 if (!session) {
172 printf("Failed to connect to device.\n");
173 return 1;
174 }
175
176 async_exch_t *exch = async_exchange_begin(session);
177 if (!exch) {
178 printf("Failed to start session exchange.\n");
179 async_hangup(session);
180 return 1;
181 }
182 const char* info = NULL;
183 ret = audio_pcm_buffer_get_info_str(exch, &info);
184 if (ret != EOK) {
185 printf("Failed to get PCM info.\n");
186 async_exchange_end(exch);
187 async_hangup(session);
188 return 1;
189 }
190 printf("Playing on %s.\n", info);
191 free(info);
192
193 playback_t pb;
194 playback_initialize(&pb, exch);
195
196 ret = audio_pcm_buffer_get_buffer(pb.device, &pb.buffer.base,
197 &pb.buffer.size, &pb.buffer.id, device_event_callback, &pb);
198 if (ret != EOK) {
199 printf("Failed to get PCM buffer: %s.\n", str_error(ret));
200 async_exchange_end(exch);
201 async_hangup(session);
202 return 1;
203 }
204 printf("Buffer (%u): %p %zu.\n", pb.buffer.id, pb.buffer.base,
205 pb.buffer.size);
206 uintptr_t ptr = 0;
207 as_get_physical_mapping(pb.buffer.base, &ptr);
208 printf("buffer mapped at %x.\n", ptr);
209
210 pb.source = fopen(file, "rb");
211 if (pb.source == NULL) {
212 printf("Failed to open %s.\n", file);
213 munmap(pb.buffer.base, pb.buffer.size);
214 audio_pcm_buffer_release_buffer(exch, pb.buffer.id);
215 async_exchange_end(exch);
216 async_hangup(session);
217 return 1;
218 }
219 wave_header_t header;
220 fread(&header, sizeof(header), 1, pb.source);
221 unsigned rate, sample_size, channels;
222 bool sign;
223 const char *error;
224 ret = wav_parse_header(&header, NULL, NULL, &rate, &sample_size,
225 &channels, &sign, &error);
226 if (ret != EOK) {
227 printf("Error parsing wav header: %s.\n", error);
228 fclose(pb.source);
229 munmap(pb.buffer.base, pb.buffer.size);
230 audio_pcm_buffer_release_buffer(exch, pb.buffer.id);
231 async_exchange_end(exch);
232 async_hangup(session);
233 return 1;
234 }
235
236 play(&pb, rate, sample_size, channels, sign);
237
238 munmap(pb.buffer.base, pb.buffer.size);
239 audio_pcm_buffer_release_buffer(exch, pb.buffer.id);
240 async_exchange_end(exch);
241 async_hangup(session);
242 return 0;
243}
244/**
245 * @}
246 */
Note: See TracBrowser for help on using the repository browser.