1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
35 #include "pcm_sampr.h"
37 /*#define LOGF_ENABLE*/
42 extern bool debug_audio
;
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
65 static SDL_AudioSpec obtained
;
66 static SDL_AudioCVT cvt
;
68 void pcm_play_lock(void)
73 void pcm_play_unlock(void)
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
);
84 cvt
.len_ratio
= (double)obtained
.freq
/ (double)pcm_sampr
;
88 void pcm_dma_apply_settings(void)
91 pcm_dma_apply_settings_nolock();
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
;
105 void pcm_play_dma_stop(void)
109 if (udata
.debug
!= NULL
) {
112 DEBUGF("Audio debug file closed\n");
117 void pcm_play_dma_pause(bool pause
)
125 size_t pcm_get_bytes_waiting(void)
127 return pcm_data_size
;
130 static void write_to_soundcard(struct pcm_udata
*udata
)
133 if (debug_audio
&& (udata
->debug
== NULL
)) {
134 udata
->debug
= fopen("audiodebug.raw", "ab");
135 DEBUGF("Audio debug file open\n");
139 Uint32 rd
= udata
->num_in
;
140 Uint32 wr
= (double)rd
* cvt
.len_ratio
;
142 if (wr
> udata
->num_out
) {
144 rd
= (double)wr
/ cvt
.len_ratio
;
146 if (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;
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
;
172 if (udata
->debug
!= NULL
) {
173 fwrite(cvt
.buf
, sizeof(Uint8
), cvt
.len_cvt
, udata
->debug
);
179 /* Convert is bad, so do silence */
180 Uint32 num
= wr
*obtained
.channels
;
184 switch (pcm_channel_bytes
)
188 Uint8
*stream
= udata
->stream
;
190 *stream
++ = obtained
.silence
;
195 Uint16
*stream
= (Uint16
*)udata
->stream
;
197 *stream
++ = obtained
.silence
;
202 if (udata
->debug
!= NULL
) {
203 fwrite(udata
->stream
, sizeof(Uint8
), wr
, udata
->debug
);
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
);
212 if (udata
->debug
!= NULL
) {
213 fwrite(pcm_data
, sizeof(Uint8
), udata
->num_out
* pcm_sample_bytes
,
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)
229 /* Audio card wants more? Get some more then. */
231 pcm_play_get_more_callback(&pcm_data
, &pcm_data_size
);
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
;
247 DEBUGF("sdl_audio_callback: No Data.\n");
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
)
283 void pcm_rec_dma_stop(void)
287 unsigned long pcm_rec_status(void)
292 const void * pcm_rec_dma_get_peak_buffer(void)
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");
307 SDL_AudioSpec wanted_spec
;
311 udata
.debug
= fopen("audiodebug.raw", "wb");
312 DEBUGF("Audio debug file open\n");
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());
331 switch (obtained
.format
)
335 pcm_channel_bytes
= 1;
341 pcm_channel_bytes
= 2;
344 DEBUGF("Unknown sample format obtained: %u\n",
345 (unsigned)obtained
.format
);
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
)
363 #endif /* CONFIG_CODEC == SWCODEC */