1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2009 Wincent Balin
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 ****************************************************************************/
23 #include "../../pdbox.h"
28 /* Extern variables. */
29 extern float sys_dacsr
;
30 extern t_sample
*sys_soundout
;
31 extern t_sample
*sys_soundin
;
35 static struct audio_buffer outbuf
[OUTBUFSIZE
];
36 static unsigned int outbuf_head
;
37 static unsigned int outbuf_tail
;
38 static unsigned int outbuf_fill
;
45 void rockbox_open_audio(int rate
)
52 /* Stop playing to reconfigure audio settings. */
55 #if INPUT_SRC_CAPS != 0
57 rb
->audio_set_input_source(AUDIO_SRC_PLAYBACK
, SRCF_PLAYBACK
);
58 rb
->audio_set_output_source(AUDIO_SRC_PLAYBACK
);
61 /* Set sample rate of the audio buffer. */
62 rb
->pcm_set_frequency(rate
);
63 rb
->pcm_apply_settings();
65 /* Initialize output buffer. */
66 for(i
= 0; i
< OUTBUFSIZE
; i
++)
75 void rockbox_close_audio(void)
80 /* Reset playing status. */
83 /* Restore default sampling rate. */
84 rb
->pcm_set_frequency(HW_SAMPR_DEFAULT
);
85 rb
->pcm_apply_settings();
88 /* Rockbox audio callback. */
89 void pdbox_get_more(unsigned char** start
, size_t* size
)
93 /* Store output data address and size. */
94 *start
= (unsigned char*) outbuf
[outbuf_tail
].data
;
95 *size
= sizeof(outbuf
[outbuf_tail
].data
);
97 /* Free this part of output buffer. */
98 outbuf
[outbuf_tail
].fill
= 0;
100 /* Advance to the next part of output buffer. */
101 if(outbuf_tail
== OUTBUFSIZE
-1)
106 /* Decrease output buffer fill. */
111 /* Reset playing status. */
114 /* Nothing to play. */
121 int rockbox_send_dacs(void)
123 /* Copy sys_output buffer. */
124 t_sample
* left
= sys_soundout
+ DEFDACBLKSIZE
*0;
125 t_sample
* right
= sys_soundout
+ DEFDACBLKSIZE
*1;
126 unsigned int samples_out
= 0;
130 /* Cancel if whole buffer filled. */
131 if(outbuf_fill
>= OUTBUFSIZE
-1)
134 /* Write the block of sound. */
136 for(out
= outbuf
[outbuf_head
].data
+
137 outbuf
[outbuf_head
].fill
* PD_OUT_CHANNELS
;
138 outbuf
[outbuf_head
].fill
< (AUDIOBUFSIZE
/ PD_OUT_CHANNELS
) &&
139 samples_out
< DEFDACBLKSIZE
;
140 left
++, right
++, samples_out
++, outbuf
[outbuf_head
].fill
++)
142 /* Copy samples from both channels. */
143 sample
= SCALE16(*left
);
146 else if(sample
< -32767)
149 sample
= SCALE16(*right
);
152 else if(sample
< -32767)
157 /* If part of output buffer filled... */
158 if(outbuf
[outbuf_head
].fill
>= (AUDIOBUFSIZE
/ PD_OUT_CHANNELS
))
160 /* Advance one part of output buffer. */
161 if(outbuf_head
== OUTBUFSIZE
-1)
166 /* Increase fill counter. */
170 /* If needed, fill the next frame. */
171 if(samples_out
< DEFDACBLKSIZE
)
174 /* Clear Pure Data output buffer. */
177 sizeof(t_sample
) * DEFDACBLKSIZE
* PD_OUT_CHANNELS
);
179 /* If still not playing... */
180 if(!playing
&& outbuf_fill
> 0)
183 rb
->pcm_play_data(pdbox_get_more
, NULL
, 0);
185 /* Set status flag. */