Skip to content

Commit

Permalink
Improve robustness of sound capabilities and other minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mesheets committed Mar 7, 2020
1 parent 2fc3406 commit 63a9049
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 61 deletions.
9 changes: 7 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CC=gcc
CFLAGS=-O2 -g -Wall -Wmissing-prototypes
TOOLPREFIX=/usr/local/brickos/bin/h8300-hitachi-hms-
CFLAGS=-O2 -pg -g -Wall -Wmissing-prototypes
#TOOLPREFIX=/usr/local/brickos/bin/h8300-hitachi-hms-
TOOLPREFIX=/usr/local/rcx/bin/h8300-hitachi-hms-
LIBS=
PROFILE=

Expand All @@ -9,8 +10,12 @@ LIBS += -L/usr/lib -lasound
SOUND_SOURCES=sound_alsa.c
else
ifneq ($(shell which sdl-config 2>/dev/null),)
# use default SDL config arguments
CFLAGS += $(shell sdl-config --cflags)
LIBS += $(shell sdl-config --libs)
# use custom SDL config arguments (e.g. for building in Cygwin)
#CFLAGS += -I/usr/local/include/SDL
#LIBS += -L/usr/local/lib -lSDL
SOUND_SOURCES=sound_sdl.c
else
SOUND_SOURCES=sound_none.c
Expand Down
26 changes: 14 additions & 12 deletions ir-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,15 @@ int main() {
} else {
int j;
for (j = 1; j < MAX_CLIENTS; j++) {
if (i != j && sockets[j] >= 0) {
if (write(sockets[j], buff, len) < 0) {
close(sockets[j]);
sockets[j] = -1;
check_exit();
}
// write to other sockets
if (i != j && sockets[j] >= 0) {
if (write(sockets[j], buff, len) < 0) {
close(sockets[j]);
sockets[j] = -1;
check_exit();
}
}
}
}
}
#if 0
usleep(1000000 * SLOT_LEN / BAUD_RATE * len);
Expand All @@ -145,21 +146,22 @@ int main() {
struct timeval current_time;
gettimeofday(&current_time, NULL);
if (next_time.tv_sec > current_time.tv_sec
|| (next_time.tv_sec == current_time.tv_sec &&
next_time.tv_usec > current_time.tv_usec)) {
usleep((next_time.tv_sec - current_time.tv_sec)
|| (next_time.tv_sec == current_time.tv_sec
&& next_time.tv_usec > current_time.tv_usec)) {
usleep((next_time.tv_sec - current_time.tv_sec)
* 1000000
+ next_time.tv_usec - current_time.tv_usec);
}
gettimeofday(&current_time, NULL);
next_time = current_time;
next_time.tv_usec += 1000000 * SLOT_LEN * len / BAUD_RATE;
if (next_time.tv_usec > 1000000) {
next_time.tv_sec++;
next_time.tv_usec -= 1000000;
next_time.tv_sec++;
next_time.tv_usec -= 1000000;
}
}
#endif
// echo back to self
if (write(sockets[i], buff, len) < 0) {
close(sockets[i]);
sockets[i] = -1;
Expand Down
7 changes: 6 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include "h8300.h"
#include "peripherals.h"


/** \file main.c
* \brief main program to start emulator and gui.
*
Expand Down Expand Up @@ -89,16 +88,22 @@ int main(int argc, char**argv) {
savefile_init();
t16_init();
t8_init();
printf("BrickEmu: Preparing to Initialize Sound\n");
sound_init();
printf("BrickEmu: Sound Initialized\n");
btn_init();
printf("BrickEmu: Buttons Initialized\n");
lcd_init();
printf("BrickEmu: LCD Initialized\n");
ws_init();
ad_init();
wdog_init();
firm_init();
motor_init();
bibo_init();

printf("BrickEmu: Initialization Complete\n");

if (argc > 1 && strcmp(argv[1], "-d") == 0)
db_trap = TRAP_EXCEPTION;
run_cpu();
Expand Down
130 changes: 86 additions & 44 deletions sound_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* 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 $
*/

Expand All @@ -30,55 +32,65 @@
#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 void sound_send_samples(void *buff, Uint8 *data, int len) {
unsigned int send = sample_end;
unsigned int sstart = sample_start;
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 (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 (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 (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;

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 = sstart;
if (len > 0) {
sample_start = s_start;

if (len16 > 0) {
#if 0
printf("Buffer underflow: %d\n", len);
printf("Buffer underflow: %d\n", len);
#endif
unsigned int level = sample_level;
while (len-- > 0)
*dest++ = level;
// unsigned int level = sample_level;
unsigned int level = sdl_silence_level;
while (len16-- > 0) {
*(dest++) = level;
}
}

return;
}

Expand All @@ -88,7 +100,8 @@ void sound_update(int bit, uint32 new_incr) {
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 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)
Expand All @@ -101,17 +114,26 @@ void sound_update(int bit, uint32 new_incr) {
len = sample_start - ptr - 1;

if (len == 0) {
/* buffer overflow, move sample_start */
// 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 (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;
}
Expand All @@ -120,7 +142,24 @@ void sound_update(int bit, uint32 new_incr) {
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;
Expand All @@ -130,9 +169,12 @@ void sound_init() {
desired.userdata = NULL;

if (SDL_OpenAudio(&desired, &obtained) == 0) {
printf("SDL Audio: Obtained %dx%d %d\n",
printf("SDL Audio: Obtained %dx%d %d\n",
obtained.freq, obtained.channels, obtained.format);
SDL_PauseAudio(0);
}
SDL_PauseAudio(0);
sdl_silence_level = obtained.silence;
} else {
printf("SDL Audio: Not Obtained");
}
sample_start = sample_end = 0;
}
4 changes: 2 additions & 2 deletions types.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
* mappings may need to be changed.
*/

#ifndef _TYPES_H_
#define _TYPES_H_
#ifndef _BRICK_EMU_TYPES_H_
#define _BRICK_EMU_TYPES_H_

typedef signed char int8;
typedef unsigned char uint8;
Expand Down

0 comments on commit 63a9049

Please sign in to comment.