From 3c4cb195e09ff5a8ea6beb797172b36adb3b9978 Mon Sep 17 00:00:00 2001 From: rnlf Date: Sun, 22 Jan 2017 20:23:54 +0100 Subject: [PATCH] cleanup --- src/mixer.c | 22 ++++++++++++++------- src/mixer.h | 13 ++++++++++-- src/modules/l_sound.c | 9 ++++++++- src/modules/l_source.c | 7 ++++++- src/soundblaster.c | 45 ++++++++++++++++++++---------------------- src/soundblaster.h | 7 +++++++ src/source.c | 7 +++++++ src/source.h | 8 +++++++- 8 files changed, 82 insertions(+), 36 deletions(-) diff --git a/src/mixer.c b/src/mixer.c index 4da97cf..d60e0ef 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -1,9 +1,17 @@ +/** + * Copyright (c) 2017 rnlf + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See LICENSE for details. + */ + #include #include #include #include "mixer.h" #include "soundblaster.h" +// Configure me! #define MIXER_MAX_SOURCES 8 @@ -14,9 +22,9 @@ typedef struct { static mixed_sound_t sources[MIXER_MAX_SOURCES]; -static int activeSources = 0; -static int16_t data[SOUNDBLASTER_SAMPLES_PER_BUFFER] = {0}; -static bool canmix = true; +static int activeSources = 0; +static int16_t data[SOUNDBLASTER_SAMPLES_PER_BUFFER] = {0}; +static bool canmix = true; void mixer_init(void) { @@ -59,15 +67,16 @@ static inline int16_t mix(int32_t a, int32_t b) { void mixer_mix(void) { - if(!canmix) + if(!canmix) { return; + } - memset(data, 0, SOUNDBLASTER_SAMPLES_PER_BUFFER); + memset(data, 0, SOUNDBLASTER_SAMPLES_PER_BUFFER * sizeof(int16_t)); for(int i = 0; i < activeSources; ++i) { mixed_sound_t *snd = sources + i; int len = snd->source->sampleCount; - int16_t const* sourceBuf = snd->source->samples; + int16_t const* sourceBuf = snd->source->samples + snd->offset; if(len > SOUNDBLASTER_SAMPLES_PER_BUFFER) { len = SOUNDBLASTER_SAMPLES_PER_BUFFER; @@ -80,7 +89,6 @@ void mixer_mix(void) { snd->offset += len; } - for(int i = activeSources-1; i >= 0; --i) { mixed_sound_t *snd = sources + i; if(snd->offset == snd->source->sampleCount) { diff --git a/src/mixer.h b/src/mixer.h index d9c54ee..da955b9 100644 --- a/src/mixer.h +++ b/src/mixer.h @@ -1,11 +1,20 @@ -#pragma once +/** + * Copyright (c) 2017 rnlf + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef MIXER_H +#define MIXER_H #include #include "source.h" - void mixer_init(void); void mixer_deinit(void); int16_t const* mixer_getNextBlock(void); void mixer_play(source_t const *source); void mixer_mix(void); + +#endif diff --git a/src/modules/l_sound.c b/src/modules/l_sound.c index 926e6dd..643b52e 100644 --- a/src/modules/l_sound.c +++ b/src/modules/l_sound.c @@ -1,4 +1,9 @@ - +/** + * Copyright (c) 2017 rnlf + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See LICENSE for details. + */ #include "mixer.h" #include "luaobj.h" @@ -9,8 +14,10 @@ int l_sound_mix(lua_State *L) { return 0; } + int l_source_new(lua_State *L); + int luaopen_sound(lua_State *L) { luaL_Reg reg[] = { { "mix", l_sound_mix }, diff --git a/src/modules/l_source.c b/src/modules/l_source.c index faeb72b..21544ab 100644 --- a/src/modules/l_source.c +++ b/src/modules/l_source.c @@ -1,4 +1,9 @@ - +/** + * Copyright (c) 2017 rnlf + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See LICENSE for details. + */ #include "luaobj.h" #include "source.h" diff --git a/src/soundblaster.c b/src/soundblaster.c index bd3f5ca..f2767a8 100644 --- a/src/soundblaster.c +++ b/src/soundblaster.c @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2017 rnlf + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See LICENSE for details. + */ + #include #include #include @@ -6,21 +13,13 @@ #include #include #include - #include "soundblaster.h" #define BYTE(val, byte) (((val) >> ((byte) * 8)) & 0xFF) -static uint16_t *sampleBuffer; -static int sampleBufferSelector; -static uint16_t baseAddress; -static uint16_t irq; -static uint16_t dmaChannel; -int isrcount = 0; - #define SAMPLE_BUFFER_SIZE (SOUNDBLASTER_SAMPLES_PER_BUFFER * sizeof(uint16_t) * 2) -// SB16 port offsets +// SB16 #define BLASTER_RESET_PORT 0x6 #define BLASTER_READ_PORT 0xA #define BLASTER_WRITE_PORT 0xC @@ -34,9 +33,6 @@ int isrcount = 0; #define BLASTER_READ_BUFFER_STATUS_AVAIL 0x80 #define BLASTER_WRITE_BUFFER_STATUS_UNAVAIL 0x80 #define BLASTER_READY_BYTE 0xAA - - -// SB16 command constants #define BLASTER_SET_OUTPUT_SAMPLING_RATE 0x41 #define BLASTER_PROGRAM_16BIT_IO_CMD 0xB0 #define BLASTER_PROGRAM_16BIT_FLAG_FIFO 0x02 @@ -59,7 +55,7 @@ int isrcount = 0; #define PIC_IRQ8F_MAP 0x70 -// DMA Data +// DMA #define DMA_DIRECTION_READ_FROM_MEMORY 0x04 #define DMA_TRANSFER_MODE_BLOCK 0x80 @@ -81,13 +77,16 @@ static const struct { { 0xC8, 0xCA, 0xD4, 0xD6, 0xD8, 0x89 }, { 0xCC, 0xCE, 0xD4, 0xD6, 0xD8, 0x8A } }; -int stopDma = 0; -// ISR data -int isrInstalled = 0; +static int stopDma = 0; +static uint16_t *sampleBuffer; +static int sampleBufferSelector; +static uint16_t baseAddress; +static uint16_t irq; +static uint16_t dmaChannel; +static bool isrInstalled = false; +static int writePage = 0; static _go32_dpmi_seginfo oldBlasterHandler, newBlasterHandler; -int writePage = 0; - static soundblaster_getSampleProc getSamples; @@ -128,7 +127,6 @@ static void soundblasterISR(void) { BLASTER_MIXER_INTERRUPT_STATUS); uint8_t status = inportb(baseAddress + BLASTER_MIXER_IN_PORT); - isrcount++; if(status & BLASTER_16BIT_INTERRUPT) { if(stopDma == 1) { writeDSP(BLASTER_EXIT_AUTO_DMA); @@ -157,7 +155,6 @@ static void setBlasterISR(void) { ? PIC_IRQ07_MAP : PIC_IRQ8F_MAP; - // Wrap and install ISR _go32_dpmi_get_protected_mode_interrupt_vector(interruptVector, &oldBlasterHandler); @@ -165,7 +162,7 @@ static void setBlasterISR(void) { newBlasterHandler.pm_selector = _go32_my_cs(); _go32_dpmi_chain_protected_mode_interrupt_vector(interruptVector, &newBlasterHandler); - // PIC setup: unmask SB IRQ + // PIC: unmask SB IRQ if(irq < 8) { uint8_t irqmask = inportb(PIC1_DATA); outportb(PIC1_DATA, irqmask & ~(1< #include #include diff --git a/src/source.h b/src/source.h index 3317be4..470887f 100644 --- a/src/source.h +++ b/src/source.h @@ -1,3 +1,10 @@ +/** + * Copyright (c) 2017 rnlf + * + * This library is free software; you can redistribute it and/or modify it + * under the terms of the MIT license. See LICENSE for details. + */ + #ifndef SOURCE_H #define SOURCE_H @@ -12,5 +19,4 @@ int source_initSilence(source_t *self, int samples); char const* source_init(source_t *self, char const* filename); void source_deinit(source_t *self); - #endif