-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsound_sdl.c
180 lines (157 loc) · 5.43 KB
/
sound_sdl.c
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
/* Emulator for LEGO RCX Brick, Copyright (C) 2003 Jochen Hoenicke.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; see the file COPYING.LESSER. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Modified and extended by Matthew Sheets
*
* $Id: sound_sdl.c 111 2005-03-13 15:43:31Z hoenicke $
*/
#include <string.h>
#include <SDL.h>
#include "h8300.h"
#include "memory.h"
#include "peripherals.h"
#define SAMPLE_RATE 48000
#define CYCLES_PER_SEC_DIV_50 ((CYCLES_PER_USEC * 1000000 / 50))
#define DECAY 0xf400 /* Depends on sample rate */
#define AMPLITUDE ((0x10000 - DECAY) >> 4)
#define BRICK_SILENCE_LEVEL_POS 4075
#define BRICK_SILENCE_LEVEL_NEG -4096
#define BUFFER_LENGTH 40960
static int16 sample_buffer[BUFFER_LENGTH];
volatile static unsigned int sample_start, sample_end;
#define FRAGMENTS ((2<<16) | 7)
static int16 sample_level = 0;
static int16 sdl_silence_level;
/* The audio function callback takes the following parameters:
data: A pointer to the audio buffer to be filled
len: The length (in bytes) of the audio buffer
*/
static void sound_send_samples(void *buff, Uint8 *data, int len8) {
unsigned int s_end = sample_end;
unsigned int s_start = sample_start;
unsigned int len16 = len8 / sizeof(int16);
unsigned int size;
int16 *ptr;
int16 *dest = (int16*) data;
if (s_end < s_start) {
size = BUFFER_LENGTH - s_start;
if (size > (unsigned) len16)
size = len16;
len16 -= size;
ptr = sample_buffer + s_start;
memcpy(dest, ptr, size);
s_start = (s_start + size) % BUFFER_LENGTH;
dest += size;
ptr += size;
}
if (len16 > 0 && ((s_end - s_start) > 0)) {
size = s_end - s_start;
if (size > (unsigned) len16)
size = len16;
len16 -= size;
ptr = sample_buffer + s_start;
memcpy(dest, ptr, size);
s_start += size;
dest += size;
ptr += size;
}
sample_start = s_start;
if (len16 > 0) {
#if 0
printf("Buffer underflow: %d\n", len);
#endif
// unsigned int level = sample_level;
unsigned int level = sdl_silence_level;
while (len16-- > 0) {
*(dest++) = level;
}
}
return;
}
void sound_update(int bit, uint32 new_incr) {
static uint32 incr = 0;
int output = bit ? AMPLITUDE : -AMPLITUDE;
int level = sample_level;
// printf("Update Sound: %d for %ld (%d/%d/%d)\n", bit, incr, tcnt[0], tcora[0], tcorb[0]);
// printf("Update Sound: %d output; %d level\n", output, level);
incr += new_incr * (SAMPLE_RATE / 50);
if (incr < CYCLES_PER_SEC_DIV_50)
return;
unsigned int ptr = sample_end;
while (incr > CYCLES_PER_SEC_DIV_50) {
unsigned int len = BUFFER_LENGTH - ptr;
if (sample_start > ptr)
len = sample_start - ptr - 1;
if (len == 0) {
// buffer overflow, move sample_start
#if 0
printf("Buffer overflow: %d\n", len);
#endif
incr = 0;
}
if (is_sound_silent(output, level)){
//printf("Update Sound: %d output; %d level\n", output, level);
while (incr > CYCLES_PER_SEC_DIV_50 && len-- > 0) {
level = ((level * DECAY) >> 16) + output;
sample_buffer[ptr++] = level;
incr -= CYCLES_PER_SEC_DIV_50;
}
} else {
// printf("Background Sound (SDL Silence is %d): %d output; %d level\n", sdl_silence_level, output, level);
// Cygwin CPU usage spikes if this loop is removed
while (incr > CYCLES_PER_SEC_DIV_50 && len-- > 0) {
incr -= CYCLES_PER_SEC_DIV_50;
}
}
if (ptr == BUFFER_LENGTH) {
ptr = 0;
}
}
sample_end = ptr;
sample_level = level;
}
int is_sound_silent(int output, int level) {
if (AMPLITUDE == output){
return (BRICK_SILENCE_LEVEL_POS != (((level * DECAY) >> 16) + output));
} else if (-AMPLITUDE == output) {
return (BRICK_SILENCE_LEVEL_NEG != (((level * DECAY) >> 16) + output));
} else {
return 0;
}
}
void sound_init() {
if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
SDL_AudioSpec desired, obtained;
desired.freq = SAMPLE_RATE;
desired.format = AUDIO_S16;
desired.channels = 1;
desired.samples = 1024;
desired.callback = sound_send_samples;
desired.userdata = NULL;
if (SDL_OpenAudio(&desired, &obtained) == 0) {
printf("SDL Audio: Obtained %dx%d %d\n",
obtained.freq, obtained.channels, obtained.format);
SDL_PauseAudio(0);
sdl_silence_level = obtained.silence;
} else {
printf("SDL Audio: Not Obtained");
}
sample_start = sample_end = 0;
}