summaryrefslogtreecommitdiffstats
path: root/apps/plugins/rockboy/rbsound.c
blob: c36e24b578791fb52648b6e8cd949b0173a9118b (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
#include "rockmacros.h"
#include "defs.h"
#include "pcm.h"

struct pcm pcm IBSS_ATTR;

#define N_BUFS 2
#define BUF_SIZE 2048

bool doneplay=1;
bool bufnum=0;

static unsigned short *buf=0, *hwbuf=0;

static bool newly_started;

static void get_more(const void** start, size_t* size)
{
    memcpy(hwbuf, &buf[pcm.len*doneplay], BUF_SIZE*sizeof(short));
    *start = hwbuf;
    *size = BUF_SIZE*sizeof(short);
    doneplay=1;
}

void rockboy_pcm_init(void)
{
    if(plugbuf)
        return;

    newly_started = true;

#if defined(HW_HAVE_11) && !defined(TOSHIBA_GIGABEAT_F)
    pcm.hz = SAMPR_11;
#else
    pcm.hz = SAMPR_44;
#endif

    pcm.stereo = 1;

    pcm.len = BUF_SIZE;
    if(!buf)
    {
        buf = my_malloc(pcm.len * N_BUFS *sizeof(short));
        hwbuf = my_malloc(pcm.len *sizeof(short));

        pcm.buf = buf;
        pcm.pos = 0;
        memset(buf, 0,  pcm.len * N_BUFS*sizeof(short));
    }

    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
   
    rb->pcm_set_frequency(pcm.hz); /* 44100 22050 11025 */
}

void rockboy_pcm_close(void)
{
    memset(&pcm, 0, sizeof pcm);    
    newly_started = true;   
    rb->pcm_play_stop();    
    rb->pcm_set_frequency(HW_SAMPR_DEFAULT);
}

int rockboy_pcm_submit(void)
{
    if (!pcm.buf) return 0;
    if (pcm.pos < pcm.len) return 1;

    if(newly_started)
    {
        rb->pcm_play_data(&get_more, NULL, NULL,0);
        newly_started = false;
    }

    while (!doneplay)
    {rb->yield();}

    doneplay=0;

    pcm.pos = 0;
    return 1;
}