-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_sdl.c
138 lines (118 loc) · 3.55 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
/* 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.
*
* $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 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 void sound_send_samples(void *buff, Uint8 *data, int len) {
unsigned int send = sample_end;
unsigned int sstart = sample_start;
unsigned int size;
int16 *ptr;
int16 *dest = (int16*) data;
if (send < sstart) {
size = BUFFER_LENGTH - sstart;
if (size > (unsigned) len)
size = len;
len -= size;
ptr = sample_buffer + sstart;
sstart = (sstart + size) % BUFFER_LENGTH;
memcpy(dest, ptr, 2*size);
dest += size;
ptr += size;
}
if (len > 0) {
size = send - sstart;
if (size > (unsigned) len)
size = len;
len -= size;
ptr = sample_buffer + sstart;
sstart += size;
memcpy(dest, ptr, 2*size);
dest += size;
ptr += size;
}
sample_start = sstart;
if (len > 0) {
#if 0
printf("Buffer underflow: %d\n", len);
#endif
unsigned int level = sample_level;
while (len-- > 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]);*/
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;
}
while (incr > CYCLES_PER_SEC_DIV_50 && len-- > 0) {
level = ((level * DECAY) >> 16) + output;
sample_buffer[ptr++] = level;
incr -= CYCLES_PER_SEC_DIV_50;
}
if (ptr == BUFFER_LENGTH) {
ptr = 0;
}
}
sample_end = ptr;
sample_level = level;
}
void sound_init() {
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);
}
sample_start = sample_end = 0;
}