Forgot that the X5/M5 use a 64/64KB IRAM split, so they can't use the previous optimi...
[Rockbox.git] / uisimulator / sdl / sound.c
blob5f061695a817958fc50233050d3a91be66d41755
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Nick Lanham
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "autoconf.h"
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <memory.h>
27 #include "debug.h"
28 #include "kernel.h"
29 #include "sound.h"
31 #include "pcm.h"
32 #include "pcm_sampr.h"
33 #include "SDL.h"
35 static int cvt_status = -1;
36 static unsigned long pcm_frequency = SAMPR_44;
38 static Uint8* pcm_data;
39 static size_t pcm_data_size;
40 static size_t pcm_sample_bytes;
41 static size_t pcm_channel_bytes;
43 struct pcm_udata
45 Uint8 *stream;
46 Uint32 num_in;
47 Uint32 num_out;
48 FILE *debug;
49 } udata;
51 static SDL_AudioSpec obtained;
52 static SDL_AudioCVT cvt;
54 extern bool debug_audio;
56 #ifndef MIN
57 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
58 #endif
60 void pcm_play_lock(void)
62 SDL_LockAudio();
65 void pcm_play_unlock(void)
67 SDL_UnlockAudio();
70 static void pcm_apply_settings_nolock(void)
72 cvt_status = SDL_BuildAudioCVT(&cvt, AUDIO_S16SYS, 2, pcm_frequency,
73 obtained.format, obtained.channels, obtained.freq);
75 pcm_curr_sampr = pcm_frequency;
77 if (cvt_status < 0) {
78 cvt.len_ratio = (double)obtained.freq / (double)pcm_curr_sampr;
82 void pcm_apply_settings(void)
84 pcm_play_lock();
85 pcm_apply_settings_nolock();
86 pcm_play_unlock();
89 void pcm_play_dma_start(const void *addr, size_t size)
91 pcm_apply_settings_nolock();
93 pcm_data = (Uint8 *) addr;
94 pcm_data_size = size;
96 SDL_PauseAudio(0);
99 void pcm_play_dma_stop(void)
101 SDL_PauseAudio(1);
104 void pcm_play_dma_pause(bool pause)
106 if (pause)
107 SDL_PauseAudio(1);
108 else
109 SDL_PauseAudio(0);
112 size_t pcm_get_bytes_waiting(void)
114 return pcm_data_size;
117 void pcm_set_frequency(unsigned int frequency)
119 switch (frequency)
121 HW_HAVE_8_( case SAMPR_8:)
122 HW_HAVE_11_(case SAMPR_11:)
123 HW_HAVE_12_(case SAMPR_12:)
124 HW_HAVE_16_(case SAMPR_16:)
125 HW_HAVE_22_(case SAMPR_22:)
126 HW_HAVE_24_(case SAMPR_24:)
127 HW_HAVE_32_(case SAMPR_32:)
128 HW_HAVE_44_(case SAMPR_44:)
129 HW_HAVE_48_(case SAMPR_48:)
130 HW_HAVE_64_(case SAMPR_64:)
131 HW_HAVE_88_(case SAMPR_88:)
132 HW_HAVE_96_(case SAMPR_96:)
133 break;
134 default:
135 frequency = SAMPR_44;
138 pcm_frequency = frequency;
141 extern int sim_volume; /* in firmware/sound.c */
142 void write_to_soundcard(struct pcm_udata *udata) {
143 if (cvt.needed) {
144 Uint32 rd = udata->num_in;
145 Uint32 wr = (double)rd * cvt.len_ratio;
147 if (wr > udata->num_out) {
148 wr = udata->num_out;
149 rd = (double)wr / cvt.len_ratio;
151 if (rd > udata->num_in)
153 rd = udata->num_in;
154 wr = (double)rd * cvt.len_ratio;
158 if (wr == 0 || rd == 0)
160 udata->num_out = udata->num_in = 0;
161 return;
164 if (cvt_status > 0) {
165 cvt.len = rd * pcm_sample_bytes;
166 cvt.buf = (Uint8 *) malloc(cvt.len * cvt.len_mult);
168 memcpy(cvt.buf, pcm_data, cvt.len);
170 SDL_ConvertAudio(&cvt);
171 SDL_MixAudio(udata->stream, cvt.buf, cvt.len_cvt, sim_volume);
173 udata->num_in = cvt.len / pcm_sample_bytes;
174 udata->num_out = cvt.len_cvt / pcm_sample_bytes;
176 if (udata->debug != NULL) {
177 fwrite(cvt.buf, sizeof(Uint8), cvt.len_cvt, udata->debug);
180 free(cvt.buf);
182 else {
183 /* Convert is bad, so do silence */
184 Uint32 num = wr*obtained.channels;
185 udata->num_in = rd;
186 udata->num_out = wr;
188 switch (pcm_channel_bytes)
190 case 1:
192 Uint8 *stream = udata->stream;
193 while (num-- > 0)
194 *stream++ = obtained.silence;
195 break;
197 case 2:
199 Uint16 *stream = (Uint16 *)udata->stream;
200 while (num-- > 0)
201 *stream++ = obtained.silence;
202 break;
206 if (udata->debug != NULL) {
207 fwrite(udata->stream, sizeof(Uint8), wr, udata->debug);
210 } else {
211 udata->num_in = udata->num_out = MIN(udata->num_in, udata->num_out);
212 SDL_MixAudio(udata->stream, pcm_data,
213 udata->num_out * pcm_sample_bytes, sim_volume);
215 if (udata->debug != NULL) {
216 fwrite(pcm_data, sizeof(Uint8), udata->num_out * pcm_sample_bytes,
217 udata->debug);
222 void sdl_audio_callback(struct pcm_udata *udata, Uint8 *stream, int len)
224 udata->stream = stream;
226 /* Write what we have in the PCM buffer */
227 if (pcm_data_size > 0)
228 goto start;
230 /* Audio card wants more? Get some more then. */
231 while (len > 0) {
232 if ((ssize_t)pcm_data_size <= 0) {
233 pcm_data_size = 0;
235 if (pcm_callback_for_more)
236 pcm_callback_for_more(&pcm_data, &pcm_data_size);
239 if (pcm_data_size > 0) {
240 start:
241 udata->num_in = pcm_data_size / pcm_sample_bytes;
242 udata->num_out = len / pcm_sample_bytes;
244 write_to_soundcard(udata);
246 udata->num_in *= pcm_sample_bytes;
247 udata->num_out *= pcm_sample_bytes;
249 pcm_data += udata->num_in;
250 pcm_data_size -= udata->num_in;
251 udata->stream += udata->num_out;
252 len -= udata->num_out;
253 } else {
254 DEBUGF("sdl_audio_callback: No Data.\n");
255 pcm_play_dma_stop();
256 pcm_play_dma_stopped_callback();
257 break;
262 const void * pcm_play_dma_get_peak_buffer(int *count)
264 uintptr_t addr = (uintptr_t)pcm_data;
265 *count = pcm_data_size / 4;
266 return (void *)((addr + 2) & ~3);
269 #ifdef HAVE_RECORDING
270 void pcm_rec_lock(void)
274 void pcm_rec_unlock(void)
278 void pcm_rec_dma_init(void)
282 void pcm_rec_dma_close(void)
286 void pcm_rec_dma_start(void *start, size_t size)
288 (void)start;
289 (void)size;
292 void pcm_rec_dma_stop(void)
296 void pcm_record_more(void *start, size_t size)
298 (void)start;
299 (void)size;
302 unsigned long pcm_rec_status(void)
304 return 0;
307 const void * pcm_rec_dma_get_peak_buffer(int *count)
309 *count = 0;
310 return NULL;
313 #endif /* HAVE_RECORDING */
315 void pcm_play_dma_init(void)
317 SDL_AudioSpec wanted_spec;
318 udata.debug = NULL;
320 if (debug_audio) {
321 udata.debug = fopen("audiodebug.raw", "wb");
324 /* Set 16-bit stereo audio at 44Khz */
325 wanted_spec.freq = 44100;
326 wanted_spec.format = AUDIO_S16SYS;
327 wanted_spec.channels = 2;
328 wanted_spec.samples = 2048;
329 wanted_spec.callback =
330 (void (SDLCALL *)(void *userdata,
331 Uint8 *stream, int len))sdl_audio_callback;
332 wanted_spec.userdata = &udata;
334 /* Open the audio device and start playing sound! */
335 if(SDL_OpenAudio(&wanted_spec, &obtained) < 0) {
336 fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
337 return;
340 switch (obtained.format)
342 case AUDIO_U8:
343 case AUDIO_S8:
344 pcm_channel_bytes = 1;
345 break;
346 case AUDIO_U16LSB:
347 case AUDIO_S16LSB:
348 case AUDIO_U16MSB:
349 case AUDIO_S16MSB:
350 pcm_channel_bytes = 2;
351 break;
352 default:
353 fprintf(stderr, "Unknown sample format obtained: %u\n",
354 (unsigned)obtained.format);
355 return;
358 pcm_sample_bytes = obtained.channels * pcm_channel_bytes;
360 pcm_apply_settings_nolock();
363 void pcm_postinit(void)