Fix pdbox makefile to actually take part in dependency generation
[kugel-rb.git] / apps / plugins / pdbox / PDa / src / s_audio_rockbox.c
blob56fa2060b4dffa98457baf9d5ceae08fe487f647
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include "plugin.h"
23 #include "../../pdbox.h"
25 #include "m_pd.h"
26 #include "s_stuff.h"
28 /* Extern variables. */
29 extern float sys_dacsr;
30 extern t_sample *sys_soundout;
31 extern t_sample *sys_soundin;
33 /* Output buffer. */
34 #define OUTBUFSIZE 3
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;
40 /* Playing status. */
41 static bool playing;
44 /* Open audio. */
45 void rockbox_open_audio(int rate)
47 unsigned int i;
49 /* No sound yet. */
50 playing = false;
52 /* Stop playing to reconfigure audio settings. */
53 rb->pcm_play_stop();
55 #if INPUT_SRC_CAPS != 0
56 /* Select playback */
57 rb->audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
58 rb->audio_set_output_source(AUDIO_SRC_PLAYBACK);
59 #endif
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++)
67 outbuf[i].fill = 0;
69 outbuf_head = 0;
70 outbuf_tail = 0;
71 outbuf_fill = 0;
74 /* Close audio. */
75 void rockbox_close_audio(void)
77 /* Stop playback. */
78 rb->pcm_play_stop();
80 /* Reset playing status. */
81 playing = false;
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)
91 if(outbuf_fill > 0)
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)
102 outbuf_tail = 0;
103 else
104 outbuf_tail++;
106 /* Decrease output buffer fill. */
107 outbuf_fill--;
109 else
111 /* Reset playing status. */
112 playing = false;
114 /* Nothing to play. */
115 *start = NULL;
116 *size = 0;
120 /* Audio I/O. */
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;
127 int16_t* out;
128 int sample;
130 /* Cancel if whole buffer filled. */
131 if(outbuf_fill >= OUTBUFSIZE-1)
132 return SENDDACS_NO;
134 /* Write the block of sound. */
135 write_block:
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);
144 if(sample > 32767)
145 sample = 32767;
146 else if(sample < -32767)
147 sample = -32767;
148 *out++ = sample;
149 sample = SCALE16(*right);
150 if(sample > 32767)
151 sample = 32767;
152 else if(sample < -32767)
153 sample = -32767;
154 *out++ = sample;
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)
162 outbuf_head = 0;
163 else
164 outbuf_head++;
166 /* Increase fill counter. */
167 outbuf_fill++;
170 /* If needed, fill the next frame. */
171 if(samples_out < DEFDACBLKSIZE)
172 goto write_block;
174 /* Clear Pure Data output buffer. */
175 memset(sys_soundout,
177 sizeof(t_sample) * DEFDACBLKSIZE * PD_OUT_CHANNELS);
179 /* If still not playing... */
180 if(!playing && outbuf_fill > 0)
182 /* Start playing. */
183 rb->pcm_play_data(pdbox_get_more, NULL, 0);
185 /* Set status flag. */
186 playing = true;
189 return SENDDACS_YES;