1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
32 #include "pcm_sampr.h"
35 static int cvt_status
= -1;
37 static Uint8
* pcm_data
;
38 static size_t pcm_data_size
;
39 static size_t pcm_sample_bytes
;
40 static size_t pcm_channel_bytes
;
50 static SDL_AudioSpec obtained
;
51 static SDL_AudioCVT cvt
;
53 extern bool debug_audio
;
56 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
59 void pcm_play_lock(void)
64 void pcm_play_unlock(void)
69 static void pcm_dma_apply_settings_nolock(void)
71 cvt_status
= SDL_BuildAudioCVT(&cvt
, AUDIO_S16SYS
, 2, pcm_sampr
,
72 obtained
.format
, obtained
.channels
, obtained
.freq
);
75 cvt
.len_ratio
= (double)obtained
.freq
/ (double)pcm_sampr
;
79 void pcm_dma_apply_settings(void)
82 pcm_dma_apply_settings_nolock();
86 void pcm_play_dma_start(const void *addr
, size_t size
)
88 pcm_dma_apply_settings_nolock();
90 pcm_data
= (Uint8
*) addr
;
96 void pcm_play_dma_stop(void)
101 void pcm_play_dma_pause(bool pause
)
109 size_t pcm_get_bytes_waiting(void)
111 return pcm_data_size
;
114 extern int sim_volume
; /* in firmware/sound.c */
115 void write_to_soundcard(struct pcm_udata
*udata
) {
117 Uint32 rd
= udata
->num_in
;
118 Uint32 wr
= (double)rd
* cvt
.len_ratio
;
120 if (wr
> udata
->num_out
) {
122 rd
= (double)wr
/ cvt
.len_ratio
;
124 if (rd
> udata
->num_in
)
127 wr
= (double)rd
* cvt
.len_ratio
;
131 if (wr
== 0 || rd
== 0)
133 udata
->num_out
= udata
->num_in
= 0;
137 if (cvt_status
> 0) {
138 cvt
.len
= rd
* pcm_sample_bytes
;
139 cvt
.buf
= (Uint8
*) malloc(cvt
.len
* cvt
.len_mult
);
141 memcpy(cvt
.buf
, pcm_data
, cvt
.len
);
143 SDL_ConvertAudio(&cvt
);
144 SDL_MixAudio(udata
->stream
, cvt
.buf
, cvt
.len_cvt
, sim_volume
);
146 udata
->num_in
= cvt
.len
/ pcm_sample_bytes
;
147 udata
->num_out
= cvt
.len_cvt
/ pcm_sample_bytes
;
149 if (udata
->debug
!= NULL
) {
150 fwrite(cvt
.buf
, sizeof(Uint8
), cvt
.len_cvt
, udata
->debug
);
156 /* Convert is bad, so do silence */
157 Uint32 num
= wr
*obtained
.channels
;
161 switch (pcm_channel_bytes
)
165 Uint8
*stream
= udata
->stream
;
167 *stream
++ = obtained
.silence
;
172 Uint16
*stream
= (Uint16
*)udata
->stream
;
174 *stream
++ = obtained
.silence
;
179 if (udata
->debug
!= NULL
) {
180 fwrite(udata
->stream
, sizeof(Uint8
), wr
, udata
->debug
);
184 udata
->num_in
= udata
->num_out
= MIN(udata
->num_in
, udata
->num_out
);
185 SDL_MixAudio(udata
->stream
, pcm_data
,
186 udata
->num_out
* pcm_sample_bytes
, sim_volume
);
188 if (udata
->debug
!= NULL
) {
189 fwrite(pcm_data
, sizeof(Uint8
), udata
->num_out
* pcm_sample_bytes
,
195 void sdl_audio_callback(struct pcm_udata
*udata
, Uint8
*stream
, int len
)
197 udata
->stream
= stream
;
199 /* Write what we have in the PCM buffer */
200 if (pcm_data_size
> 0)
203 /* Audio card wants more? Get some more then. */
205 if ((ssize_t
)pcm_data_size
<= 0) {
208 if (pcm_callback_for_more
)
209 pcm_callback_for_more(&pcm_data
, &pcm_data_size
);
212 if (pcm_data_size
> 0) {
214 udata
->num_in
= pcm_data_size
/ pcm_sample_bytes
;
215 udata
->num_out
= len
/ pcm_sample_bytes
;
217 write_to_soundcard(udata
);
219 udata
->num_in
*= pcm_sample_bytes
;
220 udata
->num_out
*= pcm_sample_bytes
;
222 pcm_data
+= udata
->num_in
;
223 pcm_data_size
-= udata
->num_in
;
224 udata
->stream
+= udata
->num_out
;
225 len
-= udata
->num_out
;
227 DEBUGF("sdl_audio_callback: No Data.\n");
229 pcm_play_dma_stopped_callback();
235 const void * pcm_play_dma_get_peak_buffer(int *count
)
237 uintptr_t addr
= (uintptr_t)pcm_data
;
238 *count
= pcm_data_size
/ 4;
239 return (void *)((addr
+ 2) & ~3);
242 #ifdef HAVE_RECORDING
243 void pcm_rec_lock(void)
247 void pcm_rec_unlock(void)
251 void pcm_rec_dma_init(void)
255 void pcm_rec_dma_close(void)
259 void pcm_rec_dma_start(void *start
, size_t size
)
265 void pcm_rec_dma_stop(void)
269 void pcm_record_more(void *start
, size_t size
)
275 unsigned long pcm_rec_status(void)
280 const void * pcm_rec_dma_get_peak_buffer(int *count
)
286 #endif /* HAVE_RECORDING */
288 void pcm_play_dma_init(void)
290 SDL_AudioSpec wanted_spec
;
294 udata
.debug
= fopen("audiodebug.raw", "wb");
297 /* Set 16-bit stereo audio at 44Khz */
298 wanted_spec
.freq
= 44100;
299 wanted_spec
.format
= AUDIO_S16SYS
;
300 wanted_spec
.channels
= 2;
301 wanted_spec
.samples
= 2048;
302 wanted_spec
.callback
=
303 (void (SDLCALL
*)(void *userdata
,
304 Uint8
*stream
, int len
))sdl_audio_callback
;
305 wanted_spec
.userdata
= &udata
;
307 /* Open the audio device and start playing sound! */
308 if(SDL_OpenAudio(&wanted_spec
, &obtained
) < 0) {
309 fprintf(stderr
, "Unable to open audio: %s\n", SDL_GetError());
313 switch (obtained
.format
)
317 pcm_channel_bytes
= 1;
323 pcm_channel_bytes
= 2;
326 fprintf(stderr
, "Unknown sample format obtained: %u\n",
327 (unsigned)obtained
.format
);
331 pcm_sample_bytes
= obtained
.channels
* pcm_channel_bytes
;
333 pcm_dma_apply_settings_nolock();
336 void pcm_postinit(void)