summaryrefslogtreecommitdiffstats
path: root/apps/plugins/pdbox/PDa/src/s_audio_rockbox.c
blob: 3c1bb6532bc218d8bc3e08f347c54b7a8de16bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2009 Wincent Balin
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "plugin.h"
#include "../../pdbox.h"

#include "m_pd.h"
#include "s_stuff.h"

/* Extern variables. */
extern float sys_dacsr;
extern t_sample *sys_soundout;
extern t_sample *sys_soundin;

/* Output buffer. */
#define OUTBUFSIZE 3
static struct audio_buffer outbuf[OUTBUFSIZE];
static unsigned int outbuf_head;
static unsigned int outbuf_tail;
static unsigned int outbuf_fill;

/* Playing status. */
static bool playing;


/* Open audio. */
void rockbox_open_audio(int rate)
{
    unsigned int i;

    /* No sound yet. */
    playing = false;

    /* Stop playing to reconfigure audio settings. */
    rb->pcm_play_stop();

#if INPUT_SRC_CAPS != 0
    /* Select playback */
    rb->audio_set_input_source(AUDIO_SRC_PLAYBACK, SRCF_PLAYBACK);
    rb->audio_set_output_source(AUDIO_SRC_PLAYBACK);
#endif

    /* Set sample rate of the audio buffer. */
    rb->pcm_set_frequency(rate);
    rb->pcm_apply_settings();

    /* Initialize output buffer. */
    for(i = 0; i < OUTBUFSIZE; i++)
        outbuf[i].fill = 0;

    outbuf_head = 0;
    outbuf_tail = 0;
    outbuf_fill = 0;
}

/* Close audio. */
void rockbox_close_audio(void)
{
    /* Stop playback. */
    rb->pcm_play_stop();

    /* Reset playing status. */
    playing = false;

    /* Restore default sampling rate. */
    rb->pcm_set_frequency(HW_SAMPR_DEFAULT);
    rb->pcm_apply_settings();
}

/* Rockbox audio callback. */
void pdbox_get_more(unsigned char** start, size_t* size)
{
    if(outbuf_fill > 0)
    {
        /* Store output data address and size. */
        *start = (unsigned char*) outbuf[outbuf_tail].data;
        *size = sizeof(outbuf[outbuf_tail].data);

        /* Free this part of output buffer. */
        outbuf[outbuf_tail].fill = 0;

        /* Advance to the next part of output buffer. */
        if(outbuf_tail == OUTBUFSIZE-1)
            outbuf_tail = 0;
        else
            outbuf_tail++;

        /* Decrease output buffer fill. */
        outbuf_fill--;
    }
    else
    {
        /* Reset playing status. */
        playing = false;

        /* Nothing to play. */
        *start = NULL;
        *size = 0;
    }
}

/* Audio I/O. */
int rockbox_send_dacs(void)
{
    /* Copy sys_output buffer. */
    t_sample* left = sys_soundout + DEFDACBLKSIZE*0;
    t_sample* right = sys_soundout + DEFDACBLKSIZE*1;
    unsigned int samples_out = 0;
    int16_t* out;
    int sample;

    /* Cancel if whole buffer filled. */
    if(outbuf_fill >= OUTBUFSIZE-1)
        return SENDDACS_NO;

    do
    {
      /* Write the block of sound. */
      for(out = outbuf[outbuf_head].data +
                outbuf[outbuf_head].fill * PD_OUT_CHANNELS;
          outbuf[outbuf_head].fill < (AUDIOBUFSIZE / PD_OUT_CHANNELS) &&
              samples_out < DEFDACBLKSIZE;
          left++, right++, samples_out++, outbuf[outbuf_head].fill++)
      {
          /* Copy samples from both channels. */
          sample = SCALE16(*left);
          if(sample > 32767)
              sample = 32767;
          else if(sample < -32767)
              sample = -32767;
          *out++ = sample;
          sample = SCALE16(*right);
          if(sample > 32767)
              sample = 32767;
          else if(sample < -32767)
              sample = -32767;
          *out++ = sample;
      }

      /* If part of output buffer filled... */
      if(outbuf[outbuf_head].fill >= (AUDIOBUFSIZE / PD_OUT_CHANNELS))
      {
          /* Advance one part of output buffer. */
          if(outbuf_head == OUTBUFSIZE-1)
              outbuf_head = 0;
          else
              outbuf_head++;

          /* Increase fill counter. */
          outbuf_fill++;
      }

    /* If needed, fill the next frame. */
    }
    while(samples_out < DEFDACBLKSIZE);

    /* Clear Pure Data output buffer. */
    memset(sys_soundout,
           0,
           sizeof(t_sample) * DEFDACBLKSIZE * PD_OUT_CHANNELS);

    /* If still not playing... */
    if(!playing && outbuf_fill > 0)
    {
        /* Start playing. */
        rb->pcm_play_data(pdbox_get_more, NULL, 0);

        /* Set status flag. */
        playing = true;
    }

    return SENDDACS_YES;
}