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;
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
;
51 static SDL_AudioSpec obtained
;
52 static SDL_AudioCVT cvt
;
54 extern bool debug_audio
;
57 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
60 void pcm_play_lock(void)
65 void pcm_play_unlock(void)
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
;
78 cvt
.len_ratio
= (double)obtained
.freq
/ (double)pcm_curr_sampr
;
82 void pcm_apply_settings(void)
85 pcm_apply_settings_nolock();
89 void pcm_play_dma_start(const void *addr
, size_t size
)
91 pcm_apply_settings_nolock();
93 pcm_data
= (Uint8
*) addr
;
99 void pcm_play_dma_stop(void)
104 void pcm_play_dma_pause(bool pause
)
112 size_t pcm_get_bytes_waiting(void)
114 return pcm_data_size
;
117 void pcm_set_frequency(unsigned int 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
:)
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
) {
144 Uint32 rd
= udata
->num_in
;
145 Uint32 wr
= (double)rd
* cvt
.len_ratio
;
147 if (wr
> udata
->num_out
) {
149 rd
= (double)wr
/ cvt
.len_ratio
;
151 if (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;
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
);
183 /* Convert is bad, so do silence */
184 Uint32 num
= wr
*obtained
.channels
;
188 switch (pcm_channel_bytes
)
192 Uint8
*stream
= udata
->stream
;
194 *stream
++ = obtained
.silence
;
199 Uint16
*stream
= (Uint16
*)udata
->stream
;
201 *stream
++ = obtained
.silence
;
206 if (udata
->debug
!= NULL
) {
207 fwrite(udata
->stream
, sizeof(Uint8
), wr
, udata
->debug
);
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
,
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)
230 /* Audio card wants more? Get some more then. */
232 if ((ssize_t
)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) {
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
;
254 DEBUGF("sdl_audio_callback: No Data.\n");
256 pcm_play_dma_stopped_callback();
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
)
292 void pcm_rec_dma_stop(void)
296 void pcm_record_more(void *start
, size_t size
)
302 unsigned long pcm_rec_status(void)
307 const void * pcm_rec_dma_get_peak_buffer(int *count
)
313 #endif /* HAVE_RECORDING */
315 void pcm_play_dma_init(void)
317 SDL_AudioSpec wanted_spec
;
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());
340 switch (obtained
.format
)
344 pcm_channel_bytes
= 1;
350 pcm_channel_bytes
= 2;
353 fprintf(stderr
, "Unknown sample format obtained: %u\n",
354 (unsigned)obtained
.format
);
358 pcm_sample_bytes
= obtained
.channels
* pcm_channel_bytes
;
360 pcm_apply_settings_nolock();
363 void pcm_postinit(void)