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

//#define ONEBUF    // Note: I think the single buffer implementation is more responsive with sound(less lag)
                  // but it creates more choppyness overall to the sound.  2 buffer's don't seem to make
                  // a difference, but 4 buffers is definately noticable

struct pcm pcm IBSS_ATTR;

bool sound = 1;
#ifdef ONEBUF
#define N_BUFS 1
#else
#define N_BUFS 4
#endif
#define BUF_SIZE 1024

rcvar_t pcm_exports[] =
{
	    RCV_END
};

#if CONFIG_CODEC == SWCODEC && !defined(SIMULATOR)

#ifndef ONEBUF
static short curbuf,gmcurbuf;
#else
bool doneplay=0;
#endif

static unsigned char *buf=0;
static unsigned short *gmbuf;

static bool newly_started;

void get_more(unsigned char** start, size_t* size)
{
#ifdef ONEBUF
   doneplay=1;
   *start = (unsigned char*)(gmbuf);
#else
   *start = (unsigned char*)(&gmbuf[pcm.len*curbuf]);
#endif
   *size = BUF_SIZE*sizeof(short);
}

void pcm_init(void)
{
	if(!sound) return;

   newly_started = true;
    
   pcm.hz = 11025;
   pcm.stereo = 1;

	pcm.len = BUF_SIZE;
	if(!buf){
      buf = my_malloc(pcm.len * N_BUFS);
	   gmbuf = my_malloc(pcm.len * N_BUFS*sizeof (short));
	   pcm.buf = buf;
	   pcm.pos = 0;
#ifndef ONEBUF
      curbuf = gmcurbuf= 0;
#endif
      memset(gmbuf, 0, pcm.len * N_BUFS *sizeof(short));
      memset(buf, 0,  pcm.len * N_BUFS);
	}

   rb->pcm_play_stop();
   
   rb->pcm_set_frequency(11025); // 44100 22050 11025
}

void pcm_close(void)
{
   memset(&pcm, 0, sizeof pcm);    
   newly_started = true;   
   rb->pcm_play_stop();	
	rb->pcm_set_frequency(44100);
}
            
int pcm_submit(void)
{
	register int i;

	if (!sound) {
		pcm.pos = 0;
		return 0;
   }

   if (pcm.pos < pcm.len) return 1;

#ifndef ONEBUF
   curbuf = (curbuf + 1) % N_BUFS;
   pcm.buf = buf + pcm.len * curbuf;
#endif
   pcm.pos = 0;

   // gotta convert the 8 bit buffer to 16
   for(i=0; i<pcm.len;i++)
#ifdef ONEBUF
      gmbuf[i] = (pcm.buf[i]<<8)-0x8000;
#else
      gmbuf[i+pcm.len*curbuf] = (pcm.buf[i]<<8)-0x8000;
#endif
    
   if(newly_started)
   {  
      rb->pcm_play_data(&get_more,NULL,0);
      newly_started = false;
   }    
   
   // this while loop and done play are in place to make sure the sound timing is correct(although it's not)
#ifdef ONEBUF
   while(doneplay==0) rb->yield();
   doneplay=0;
#endif
   return 1;
}
#else
static byte buf1_unal[(BUF_SIZE / sizeof(short)) + 2]; // to make sure 4 byte aligned
void pcm_init(void)
{
    pcm.hz = 11025;
    pcm.stereo = 1;
    pcm.buf = buf1_unal;
    pcm.len = (BUF_SIZE / sizeof(short));
    pcm.pos = 0;
}

void pcm_close(void)
{
    memset(&pcm, 0, sizeof pcm);
}
int pcm_submit(void)
{
    pcm.pos =0;
    return 0;
}
#endif