Make PCM->driver interface about as simple as it will get. Registered callback, zero...
[kugel-rb.git] / firmware / target / hosted / sdl / pcm-sdl.c
blob03e6e1336c56036cd9dc369c02c06e695373d629
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Nick Lanham
11 * Copyright (C) 2010 by Thomas Martitz
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
23 #include "autoconf.h"
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <SDL.h>
28 #include "config.h"
29 #include "debug.h"
30 #include "sound.h"
31 #include "audiohw.h"
32 #include "system.h"
34 #include "pcm.h"
35 #include "pcm_sampr.h"
37 /*#define LOGF_ENABLE*/
38 #include "logf.h"
40 #ifdef DEBUG
41 #include <stdio.h>
42 extern bool debug_audio;
43 #endif
45 static int sim_volume = 0;
47 #if CONFIG_CODEC == SWCODEC
48 static int cvt_status = -1;
50 static Uint8* pcm_data;
51 static size_t pcm_data_size;
52 static size_t pcm_sample_bytes;
53 static size_t pcm_channel_bytes;
55 static struct pcm_udata
57 Uint8 *stream;
58 Uint32 num_in;
59 Uint32 num_out;
60 #ifdef DEBUG
61 FILE *debug;
62 #endif
63 } udata;
65 static SDL_AudioSpec obtained;
66 static SDL_AudioCVT cvt;
68 void pcm_play_lock(void)
70 SDL_LockAudio();
73 void pcm_play_unlock(void)
75 SDL_UnlockAudio();
78 static void pcm_dma_apply_settings_nolock(void)
80 cvt_status = SDL_BuildAudioCVT(&cvt, AUDIO_S16SYS, 2, pcm_sampr,
81 obtained.format, obtained.channels, obtained.freq);
83 if (cvt_status < 0) {
84 cvt.len_ratio = (double)obtained.freq / (double)pcm_sampr;
88 void pcm_dma_apply_settings(void)
90 pcm_play_lock();
91 pcm_dma_apply_settings_nolock();
92 pcm_play_unlock();
95 void pcm_play_dma_start(const void *addr, size_t size)
97 pcm_dma_apply_settings_nolock();
99 pcm_data = (Uint8 *) addr;
100 pcm_data_size = size;
102 SDL_PauseAudio(0);
105 void pcm_play_dma_stop(void)
107 SDL_PauseAudio(1);
108 #ifdef DEBUG
109 if (udata.debug != NULL) {
110 fclose(udata.debug);
111 udata.debug = NULL;
112 DEBUGF("Audio debug file closed\n");
114 #endif
117 void pcm_play_dma_pause(bool pause)
119 if (pause)
120 SDL_PauseAudio(1);
121 else
122 SDL_PauseAudio(0);
125 size_t pcm_get_bytes_waiting(void)
127 return pcm_data_size;
130 static void write_to_soundcard(struct pcm_udata *udata)
132 #ifdef DEBUG
133 if (debug_audio && (udata->debug == NULL)) {
134 udata->debug = fopen("audiodebug.raw", "ab");
135 DEBUGF("Audio debug file open\n");
137 #endif
138 if (cvt.needed) {
139 Uint32 rd = udata->num_in;
140 Uint32 wr = (double)rd * cvt.len_ratio;
142 if (wr > udata->num_out) {
143 wr = udata->num_out;
144 rd = (double)wr / cvt.len_ratio;
146 if (rd > udata->num_in)
148 rd = udata->num_in;
149 wr = (double)rd * cvt.len_ratio;
153 if (wr == 0 || rd == 0)
155 udata->num_out = udata->num_in = 0;
156 return;
159 if (cvt_status > 0) {
160 cvt.len = rd * pcm_sample_bytes;
161 cvt.buf = (Uint8 *) malloc(cvt.len * cvt.len_mult);
163 memcpy(cvt.buf, pcm_data, cvt.len);
165 SDL_ConvertAudio(&cvt);
166 SDL_MixAudio(udata->stream, cvt.buf, cvt.len_cvt, sim_volume);
168 udata->num_in = cvt.len / pcm_sample_bytes;
169 udata->num_out = cvt.len_cvt / pcm_sample_bytes;
171 #ifdef DEBUG
172 if (udata->debug != NULL) {
173 fwrite(cvt.buf, sizeof(Uint8), cvt.len_cvt, udata->debug);
175 #endif
176 free(cvt.buf);
178 else {
179 /* Convert is bad, so do silence */
180 Uint32 num = wr*obtained.channels;
181 udata->num_in = rd;
182 udata->num_out = wr;
184 switch (pcm_channel_bytes)
186 case 1:
188 Uint8 *stream = udata->stream;
189 while (num-- > 0)
190 *stream++ = obtained.silence;
191 break;
193 case 2:
195 Uint16 *stream = (Uint16 *)udata->stream;
196 while (num-- > 0)
197 *stream++ = obtained.silence;
198 break;
201 #ifdef DEBUG
202 if (udata->debug != NULL) {
203 fwrite(udata->stream, sizeof(Uint8), wr, udata->debug);
205 #endif
207 } else {
208 udata->num_in = udata->num_out = MIN(udata->num_in, udata->num_out);
209 SDL_MixAudio(udata->stream, pcm_data,
210 udata->num_out * pcm_sample_bytes, sim_volume);
211 #ifdef DEBUG
212 if (udata->debug != NULL) {
213 fwrite(pcm_data, sizeof(Uint8), udata->num_out * pcm_sample_bytes,
214 udata->debug);
216 #endif
220 static void sdl_audio_callback(struct pcm_udata *udata, Uint8 *stream, int len)
222 logf("sdl_audio_callback: len %d, pcm %d\n", len, pcm_data_size);
223 udata->stream = stream;
225 /* Write what we have in the PCM buffer */
226 if (pcm_data_size > 0)
227 goto start;
229 /* Audio card wants more? Get some more then. */
230 while (len > 0) {
231 pcm_play_get_more_callback(&pcm_data, &pcm_data_size);
232 start:
233 if (pcm_data_size != 0) {
234 udata->num_in = pcm_data_size / pcm_sample_bytes;
235 udata->num_out = len / pcm_sample_bytes;
237 write_to_soundcard(udata);
239 udata->num_in *= pcm_sample_bytes;
240 udata->num_out *= pcm_sample_bytes;
242 pcm_data += udata->num_in;
243 pcm_data_size -= udata->num_in;
244 udata->stream += udata->num_out;
245 len -= udata->num_out;
246 } else {
247 DEBUGF("sdl_audio_callback: No Data.\n");
248 break;
253 const void * pcm_play_dma_get_peak_buffer(int *count)
255 uintptr_t addr = (uintptr_t)pcm_data;
256 *count = pcm_data_size / 4;
257 return (void *)((addr + 2) & ~3);
260 #ifdef HAVE_RECORDING
261 void pcm_rec_lock(void)
265 void pcm_rec_unlock(void)
269 void pcm_rec_dma_init(void)
273 void pcm_rec_dma_close(void)
277 void pcm_rec_dma_start(void *start, size_t size)
279 (void)start;
280 (void)size;
283 void pcm_rec_dma_stop(void)
287 unsigned long pcm_rec_status(void)
289 return 0;
292 const void * pcm_rec_dma_get_peak_buffer(void)
294 return NULL;
297 #endif /* HAVE_RECORDING */
299 void pcm_play_dma_init(void)
301 if (SDL_InitSubSystem(SDL_INIT_AUDIO))
303 DEBUGF("Could not initialize SDL audio subsystem!\n");
304 return;
307 SDL_AudioSpec wanted_spec;
308 #ifdef DEBUG
309 udata.debug = NULL;
310 if (debug_audio) {
311 udata.debug = fopen("audiodebug.raw", "wb");
312 DEBUGF("Audio debug file open\n");
314 #endif
315 /* Set 16-bit stereo audio at 44Khz */
316 wanted_spec.freq = 44100;
317 wanted_spec.format = AUDIO_S16SYS;
318 wanted_spec.channels = 2;
319 wanted_spec.samples = 2048;
320 wanted_spec.callback =
321 (void (SDLCALL *)(void *userdata,
322 Uint8 *stream, int len))sdl_audio_callback;
323 wanted_spec.userdata = &udata;
325 /* Open the audio device and start playing sound! */
326 if(SDL_OpenAudio(&wanted_spec, &obtained) < 0) {
327 DEBUGF("Unable to open audio: %s\n", SDL_GetError());
328 return;
331 switch (obtained.format)
333 case AUDIO_U8:
334 case AUDIO_S8:
335 pcm_channel_bytes = 1;
336 break;
337 case AUDIO_U16LSB:
338 case AUDIO_S16LSB:
339 case AUDIO_U16MSB:
340 case AUDIO_S16MSB:
341 pcm_channel_bytes = 2;
342 break;
343 default:
344 DEBUGF("Unknown sample format obtained: %u\n",
345 (unsigned)obtained.format);
346 return;
349 pcm_sample_bytes = obtained.channels * pcm_channel_bytes;
351 pcm_dma_apply_settings_nolock();
354 void pcm_postinit(void)
358 void pcm_set_mixer_volume(int volume)
360 sim_volume = volume;
363 #endif /* CONFIG_CODEC == SWCODEC */