diff options
Diffstat (limited to 'apps/plugins/rockboy/rbsound.c')
-rw-r--r-- | apps/plugins/rockboy/rbsound.c | 113 |
1 files changed, 73 insertions, 40 deletions
diff --git a/apps/plugins/rockboy/rbsound.c b/apps/plugins/rockboy/rbsound.c index b68a053f77..3eebea8bef 100644 --- a/apps/plugins/rockboy/rbsound.c +++ b/apps/plugins/rockboy/rbsound.c | |||
@@ -3,10 +3,18 @@ | |||
3 | #include "pcm.h" | 3 | #include "pcm.h" |
4 | #include "rc.h" | 4 | #include "rc.h" |
5 | 5 | ||
6 | struct pcm pcm; | 6 | //#define ONEBUF // Note: I think the single buffer implementation is more responsive with sound(less lag) |
7 | // but it creates more choppyness overall to the sound. 2 buffer's don't seem to make | ||
8 | // a difference, but 4 buffers is definately noticable | ||
9 | |||
10 | struct pcm pcm IBSS_ATTR; | ||
7 | 11 | ||
8 | bool sound = 1; | 12 | bool sound = 1; |
13 | #ifdef ONEBUF | ||
14 | #define N_BUFS 1 | ||
15 | #else | ||
9 | #define N_BUFS 4 | 16 | #define N_BUFS 4 |
17 | #endif | ||
10 | #define BUF_SIZE 1024 | 18 | #define BUF_SIZE 1024 |
11 | 19 | ||
12 | rcvar_t pcm_exports[] = | 20 | rcvar_t pcm_exports[] = |
@@ -16,48 +24,62 @@ rcvar_t pcm_exports[] = | |||
16 | 24 | ||
17 | #if CONFIG_CODEC == SWCODEC && !defined(SIMULATOR) | 25 | #if CONFIG_CODEC == SWCODEC && !defined(SIMULATOR) |
18 | 26 | ||
19 | static int curbuf,gmcurbuf; | 27 | #ifndef ONEBUF |
28 | static short curbuf,gmcurbuf; | ||
29 | #else | ||
30 | bool doneplay=0; | ||
31 | #endif | ||
20 | 32 | ||
21 | static byte *buf=0; | 33 | static unsigned char *buf=0; |
22 | static short *gmbuf; | 34 | static unsigned short *gmbuf; |
23 | 35 | ||
24 | static bool newly_started; | 36 | static bool newly_started; |
25 | 37 | ||
38 | void get_more(unsigned char** start, long* size) | ||
39 | { | ||
40 | #ifdef ONEBUF | ||
41 | doneplay=1; | ||
42 | *start = (unsigned char*)(gmbuf); | ||
43 | #else | ||
44 | *start = (unsigned char*)(&gmbuf[pcm.len*curbuf]); | ||
45 | #endif | ||
46 | *size = BUF_SIZE*sizeof(short); | ||
47 | } | ||
48 | |||
26 | void pcm_init(void) | 49 | void pcm_init(void) |
27 | { | 50 | { |
28 | if(!sound) return; | 51 | if(!sound) return; |
29 | 52 | ||
30 | newly_started = true; | 53 | newly_started = true; |
31 | 54 | ||
32 | pcm.hz = 11025; | 55 | pcm.hz = 11025; |
33 | pcm.stereo = 1; | 56 | pcm.stereo = 1; |
34 | 57 | ||
35 | pcm.len = BUF_SIZE; | 58 | pcm.len = BUF_SIZE; |
36 | if(!buf){ | 59 | if(!buf){ |
37 | buf = my_malloc(pcm.len * N_BUFS); | 60 | buf = my_malloc(pcm.len * N_BUFS); |
38 | gmbuf = my_malloc(pcm.len * N_BUFS*sizeof (short)); | 61 | gmbuf = my_malloc(pcm.len * N_BUFS*sizeof (short)); |
39 | pcm.buf = buf; | 62 | pcm.buf = buf; |
40 | pcm.pos = 0; | 63 | pcm.pos = 0; |
41 | curbuf = gmcurbuf= 0; | 64 | #ifndef ONEBUF |
65 | curbuf = gmcurbuf= 0; | ||
66 | #endif | ||
67 | memset(gmbuf, 0, pcm.len * N_BUFS *sizeof(short)); | ||
68 | memset(buf, 0, pcm.len * N_BUFS); | ||
42 | } | 69 | } |
43 | 70 | ||
44 | rb->pcm_play_stop(); | 71 | rb->pcm_play_stop(); |
45 | rb->pcm_set_frequency(11025); // 44100 22050 11025 | 72 | |
73 | rb->pcm_set_frequency(11025); // 44100 22050 11025 | ||
46 | } | 74 | } |
47 | 75 | ||
48 | void pcm_close(void) | 76 | void pcm_close(void) |
49 | { | 77 | { |
50 | memset(&pcm, 0, sizeof pcm); | 78 | memset(&pcm, 0, sizeof pcm); |
51 | newly_started = true; | 79 | newly_started = true; |
52 | rb->pcm_play_stop(); | 80 | rb->pcm_play_stop(); |
53 | rb->pcm_set_frequency(44100); | 81 | rb->pcm_set_frequency(44100); |
54 | } | 82 | } |
55 | |||
56 | void get_more(unsigned char** start, long* size) | ||
57 | { | ||
58 | *start = (unsigned char*)(&gmbuf[pcm.len*curbuf]); | ||
59 | *size = BUF_SIZE*sizeof(short); | ||
60 | } | ||
61 | 83 | ||
62 | int pcm_submit(void) | 84 | int pcm_submit(void) |
63 | { | 85 | { |
@@ -66,25 +88,36 @@ int pcm_submit(void) | |||
66 | if (!sound) { | 88 | if (!sound) { |
67 | pcm.pos = 0; | 89 | pcm.pos = 0; |
68 | return 0; | 90 | return 0; |
69 | } | 91 | } |
70 | 92 | ||
71 | if (pcm.pos >= pcm.len) { | 93 | if (pcm.pos < pcm.len) return 1; |
72 | curbuf = (curbuf + 1) % N_BUFS; | ||
73 | pcm.buf = buf + pcm.len * curbuf; | ||
74 | pcm.pos = 0; | ||
75 | 94 | ||
76 | // gotta convert the 8 bit buffer to 16 | 95 | #ifndef ONEBUF |
77 | for(i=0; i<pcm.len;i++) | 96 | curbuf = (curbuf + 1) % N_BUFS; |
78 | gmbuf[i+pcm.len*curbuf] = (pcm.buf[i]<<8)-0x8000; | 97 | pcm.buf = buf + pcm.len * curbuf; |
79 | } | 98 | #endif |
80 | 99 | pcm.pos = 0; | |
81 | if(newly_started) | 100 | |
82 | { | 101 | // gotta convert the 8 bit buffer to 16 |
83 | rb->pcm_play_data(&get_more); | 102 | for(i=0; i<pcm.len;i++) |
84 | newly_started = false; | 103 | #ifdef ONEBUF |
85 | } | 104 | gmbuf[i] = (pcm.buf[i]<<8)-0x8000; |
105 | #else | ||
106 | gmbuf[i+pcm.len*curbuf] = (pcm.buf[i]<<8)-0x8000; | ||
107 | #endif | ||
86 | 108 | ||
87 | return 1; | 109 | if(newly_started) |
110 | { | ||
111 | rb->pcm_play_data(&get_more); | ||
112 | newly_started = false; | ||
113 | } | ||
114 | |||
115 | // this while loop and done play are in place to make sure the sound timing is correct(although it's not) | ||
116 | #ifdef ONEBUF | ||
117 | while(doneplay==0) rb->yield(); | ||
118 | doneplay=0; | ||
119 | #endif | ||
120 | return 1; | ||
88 | } | 121 | } |
89 | #else | 122 | #else |
90 | static byte buf1_unal[(BUF_SIZE / sizeof(short)) + 2]; // to make sure 4 byte aligned | 123 | static byte buf1_unal[(BUF_SIZE / sizeof(short)) + 2]; // to make sure 4 byte aligned |