Clamp output

This commit is contained in:
Ian Scott committed 2024-03-25 23:53:05 -06:00
1 parent d865b1777c
commit b4257f233e
4 files changed
+44 -47

No files matched your search

+1 -1
View File
@@ -131,7 +131,7 @@ if(PROJECT_TYPE STREQUAL "OPL")
USE_ALARM=1
USE_EMU8950_OPL=1
EMU8950_ASM=1
PICOW=1
# PICOW=1
)
add_subdirectory(opl)
pico_enable_stdio_uart(picogus 1)
+30
View File
@@ -0,0 +1,30 @@
#ifdef INTERP_CLAMP
#include "hardware/interp.h"
#endif
static void clamp_setup(void) {
#ifdef INTERP_CLAMP
interp_config cfg;
// Clamp setup
cfg = interp_default_config();
interp_config_set_clamp(&cfg, true);
interp_config_set_shift(&cfg, 14);
// set mask according to new position of sign bit..
interp_config_set_mask(&cfg, 0, 17);
// ...so that the shifted value is correctly sign extended
interp_config_set_signed(&cfg, true);
interp_set_config(interp1, 0, &cfg);
interp1->base[0] = -32768;
interp1->base[1] = 32767;
#endif
}
static int16_t __force_inline clamp16(int32_t d) {
#ifdef INTERP_CLAMP
interp1->accum[0] = d;
return interp1->peek[0];
#else
return d < -32768 ? -32768 : (d > 32767 ? 32767 : d);
#endif // INTERP_CLAMP
}
+6 -43
View File
@@ -30,7 +30,7 @@
extern psram_spi_inst_t psram_spi;
#endif
#if defined(INTERP_CLAMP) || defined(INTERP_LINEAR)
#if defined(INTERP_LINEAR)
#include "hardware/interp.h"
#endif
@@ -41,6 +41,8 @@ critical_section_t gus_crit;
#include "isa_dma.h"
extern dma_inst_t dma_config;
#include "clamp.h"
using namespace std;
//Extra bits of precision over normal gus
@@ -1597,19 +1599,7 @@ __force_inline void GUS_StartDMA() {
}
#ifdef INTERP_CLAMP
static int16_t __force_inline clamp16(int32_t d) {
interp1->accum[0] = d;
return interp1->peek[0];
}
#else
static int32_t __force_inline clamp(int32_t d, int32_t min, int32_t max) {
return d < min ? min : (d > max ? max : d);
}
#endif // INTERP_CLAMP
extern uint32_t /*__scratch_x("my_sub_section")*/ (GUS_CallBack)(Bitu max_len, int16_t* play_buffer) {
extern uint32_t __scratch_x("my_sub_section") (GUS_CallBack)(Bitu max_len, int16_t* play_buffer) {
// extern uint32_t GUS_CallBack(Bitu max_len, int16_t* play_buffer) {
static int32_t accum[2];
#ifdef SCALE_22K_TO_44K
@@ -1624,7 +1614,6 @@ extern uint32_t /*__scratch_x("my_sub_section")*/ (GUS_CallBack)(Bitu max_len, i
for (Bitu c = 0; c < myGUS.ActiveChannels; ++c) {
guschan[c]->generateSample(accum);
}
#ifdef INTERP_CLAMP
#ifdef SCALE_22K_TO_44K
if (!myGUS.fixed_44k_output && myGUS.ActiveChannels == 28) {
play_buffer[s << 1] = clamp16((accum[0] + prev_accum[0]) >> 1);
@@ -1634,17 +1623,6 @@ extern uint32_t /*__scratch_x("my_sub_section")*/ (GUS_CallBack)(Bitu max_len, i
#endif // SCALE_22K_TO_44K
play_buffer[s << 1] = clamp16(accum[0]);
play_buffer[(s << 1) + 1] = clamp16(accum[1]);
#else // INTERP_CLAMP
#ifdef SCALE_22K_TO_44K
if (!myGUS.fixed_44k_output && myGUS.ActiveChannels == 28) {
play_buffer[s << 1] = clamp((accum[0] + prev_accum[0]) >> 13, -32768, 32767);
play_buffer[(s << 1) + 1] = clamp((accum[1] + prev_accum[1]) >> 13, -32768, 32767);
++s;
}
#endif // SCALE_22K_TO_44K
play_buffer[s << 1] = clamp(accum[0] >> 14, -32768, 32767);
play_buffer[(s << 1) + 1] = clamp(accum[1] >> 14, -32768, 32767);
#endif // INTERP_CLAMP
++s;
#ifdef SCALE_22K_TO_44K
prev_accum[0] = accum[0];
@@ -1887,10 +1865,8 @@ void GUS_OnReset(void) {
}
void GUS_Setup() {
#if defined(INTERP_LINEAR) || defined(INTERP_CLAMP)
#if defined(INTERP_LINEAR)
interp_config cfg;
#endif
#ifdef INTERP_LINEAR
cfg = interp_default_config();
// Linear interpolation setup
interp_config_set_blend(&cfg, true);
@@ -1900,18 +1876,5 @@ void GUS_Setup() {
interp_config_set_signed(&cfg, true);
interp_set_config(interp0, 1, &cfg);
#endif
#ifdef INTERP_CLAMP
// Clamp setup
cfg = interp_default_config();
interp_config_set_clamp(&cfg, true);
interp_config_set_shift(&cfg, 14);
// set mask according to new position of sign bit..
interp_config_set_mask(&cfg, 0, 17);
// ...so that the shifted value is correctly sign extended
interp_config_set_signed(&cfg, true);
interp_set_config(interp1, 0, &cfg);
interp1->base[0] = -32768;
interp1->base[1] = 32767;
#endif
clamp_setup();
}
+7 -3
View File
@@ -13,6 +13,7 @@ Author : Kevin Moonlight <me@yyzkevin.com>
*/
#include "isa_dma.h"
#include "clamp.h"
extern "C" void OPL_Pico_Mix_callback(audio_buffer_t *);
extern "C" void OPL_Pico_simple(int16_t *buffer, uint32_t nsamples);
@@ -214,11 +215,14 @@ void sbdsp_dma_isr(void) {
int16_t sbdsp_sample() {
int16_t snd16;
int32_t snd32;
OPL_Pico_simple(&snd16, 1);
if (sbdsp.dma_enabled) {
snd16 += (int16_t)(cur_sample)-0x80 << 4;
snd32 = ((int32_t)(cur_sample)-0x80 << 4) + snd16;
} else {
snd32 = snd16;
}
return snd16;
return clamp16(snd32);
}
void sbdsp_init() {
@@ -235,7 +239,7 @@ void sbdsp_init() {
opl_buffer->buffer->size = 512*4; // 49716/8000*64==397
opl_buffer->buffer->bytes = (uint8_t *) malloc(opl_buffer->buffer->size);
clamp_setup();
}