working on audio buffer stuff
This commit is contained in:
parent
020feb2a6f
commit
5668389488
@ -117,6 +117,11 @@ typedef struct {
|
||||
float right_history[384];
|
||||
u32 right_index;
|
||||
|
||||
u8 sq1_sample_val;
|
||||
u32 sq1_write_index;
|
||||
u32 sq1_read_index;
|
||||
u8 sq1_audio_buffer[4096];
|
||||
|
||||
} audio_context;
|
||||
|
||||
void audio_init();
|
||||
|
47
lib/audio.c
47
lib/audio.c
@ -5,11 +5,13 @@
|
||||
|
||||
#include <portaudio.h>
|
||||
#include <pa_ringbuffer.h>
|
||||
#include <math.h>
|
||||
|
||||
#define SAMPLE_RATE 44100
|
||||
#define FRAMES_PER_BUFFER 64
|
||||
#define TIME_PER_SAMPLE 1.0f / (SAMPLE_RATE/1000.0f/1000.0f)
|
||||
#define TIME_PER_AUDIO_TICK 1.0f / (1048576.0f/1000.0f/1000.0f)
|
||||
#define AUDIO_TICKS_PER_SAMPLE 1048576.0f / SAMPLE_RATE
|
||||
#define TIME_PER_WAVE_TICK 1.0f / (2097152.0f/1000.0f/1000.0f)
|
||||
#define LFSR_BASE_CLOCK 262144.0f
|
||||
|
||||
@ -31,6 +33,7 @@ static float right_cap = 0.0f;
|
||||
static int history_timer = 0;
|
||||
const int history_interval = 5;
|
||||
static int lfsr_clocks = 0;
|
||||
static double write_index = 0;
|
||||
|
||||
const u8 square_sample_00[8] = {
|
||||
0x0,
|
||||
@ -90,6 +93,12 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
|
||||
void *userData ) {
|
||||
float *out = (float *)output_buffer;
|
||||
for(int i = 0; i < framesPerBuffer; i++) {
|
||||
write_index = write_index + AUDIO_TICKS_PER_SAMPLE;
|
||||
ctx.sq1_read_index = (int)write_index;
|
||||
if(ctx.sq1_read_index > ctx.sq1_write_index) {
|
||||
ctx.sq1_read_index = ctx.sq1_write_index-1;
|
||||
write_index = ctx.sq1_read_index;
|
||||
}
|
||||
if(ppu_get_context()->paused) {
|
||||
*out++ = 0;
|
||||
*out++ = 0;
|
||||
@ -100,13 +109,13 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
|
||||
audio_time += TIME_PER_SAMPLE;
|
||||
wave_time += TIME_PER_SAMPLE;
|
||||
for(;audio_time >= TIME_PER_AUDIO_TICK;audio_time -= TIME_PER_AUDIO_TICK) {
|
||||
ctx.sq1_period_timer++;
|
||||
//ctx.sq1_period_timer++;
|
||||
ctx.sq2_period_timer++;
|
||||
|
||||
if(ctx.sq1_period_timer >= 0x800) {
|
||||
ctx.sq1_period_timer = ctx.sq1_period_reset;
|
||||
ctx.sq1_sample = (ctx.sq1_sample + 1) % 8;
|
||||
}
|
||||
//if(ctx.sq1_period_timer >= 0x800) {
|
||||
// ctx.sq1_period_timer = ctx.sq1_period_reset;
|
||||
// ctx.sq1_sample = (ctx.sq1_sample + 1) % 8;
|
||||
//}
|
||||
if(ctx.sq2_period_timer >= 0x800) {
|
||||
ctx.sq2_period_timer = ctx.sq2_period_reset;
|
||||
ctx.sq2_sample = (ctx.sq2_sample + 1) % 8;
|
||||
@ -158,7 +167,8 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
|
||||
if(ctx.ch1_dac){
|
||||
sq1_val = -1;
|
||||
if(ctx.sq1_enable) {
|
||||
sq1_val = ((float)ctx.sq1_volume/15.0f) * (((float)(square_sample[ctx.sq1_duty][ctx.sq1_sample]) - 7.5f)/7.5f);
|
||||
//sq1_val = ((float)ctx.sq1_volume/15.0f) * (((float)(square_sample[ctx.sq1_duty][ctx.sq1_sample]) - 7.5f)/7.5f);
|
||||
sq1_val = ((float)ctx.sq1_volume/15.0f) * (((float)(ctx.sq1_audio_buffer[ctx.sq1_read_index % 4096]) - 7.5f)/7.5f);
|
||||
if(ctx.ch1_left) {
|
||||
left += sq1_val;
|
||||
}else {
|
||||
@ -177,7 +187,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
|
||||
if(ctx.ch2_dac){
|
||||
sq2_val = -1;
|
||||
if(ctx.sq2_enable) {
|
||||
sq2_val = ((float)ctx.sq2_volume/15.0f) * (((float)(square_sample[ctx.sq2_duty][ctx.sq2_sample]) - 7.5f)/7.5f);
|
||||
//sq2_val = ((float)ctx.sq2_volume/15.0f) * (((float)(square_sample[ctx.sq2_duty][ctx.sq2_sample]) - 7.5f)/7.5f);
|
||||
if(ctx.ch2_left) {
|
||||
left += sq2_val;
|
||||
}else {
|
||||
@ -206,7 +216,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
|
||||
if(ctx.ch3_volume == 0b11) {
|
||||
shift = 2;
|
||||
}
|
||||
ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f);
|
||||
//ch3_val = (((float)(ctx.ch3_last_sample >> shift) - 7.5f)/7.5f);
|
||||
if(ctx.ch3_left) {
|
||||
left += ch3_val;
|
||||
//printf("left: %d\n", ctx.ch3_volume);
|
||||
@ -226,7 +236,7 @@ static int audio_callback(const void* input_uffer, void *output_buffer,
|
||||
if(ctx.ch4_dac){
|
||||
ch4_val = -1;
|
||||
if(ctx.ch4_enable) {
|
||||
ch4_val = ((ctx.ch4_lfsr & 0b1) == 0b1) ? (((float)ctx.ch4_volume - 7.5f)/7.5f) : -1.0f;
|
||||
//ch4_val = ((ctx.ch4_lfsr & 0b1) == 0b1) ? (((float)ctx.ch4_volume - 7.5f)/7.5f) : -1.0f;
|
||||
if(ctx.ch4_left) {
|
||||
left += ch4_val;
|
||||
//printf("left: %d\n", ctx.ch3_volume);
|
||||
@ -368,6 +378,7 @@ void audio_init(){
|
||||
ctx.sq2_env_direction = false;
|
||||
ctx.sq2_env_pace = 0x0;
|
||||
ctx.sq2_env_timer = 0x0;
|
||||
ctx.sq1_write_index = 1;
|
||||
|
||||
err = Pa_Initialize();
|
||||
if (err != paNoError) goto error;
|
||||
@ -412,6 +423,24 @@ void sq1_sweep() {
|
||||
static int change = 1;
|
||||
static u32 ticks = 0;
|
||||
static u32 start, end = 0;
|
||||
static u32 period_tick;
|
||||
|
||||
void audio_period_tick() {
|
||||
period_tick++;
|
||||
if(period_tick % 2 == 0){
|
||||
ctx.sq1_period_timer++;
|
||||
|
||||
if(ctx.sq1_period_timer >= 0x800) {
|
||||
ctx.sq1_period_timer = ctx.sq1_period_reset;
|
||||
ctx.sq1_sample = (ctx.sq1_sample + 1) % 8;
|
||||
}
|
||||
if(ctx.sq1_write_index > ctx.sq1_read_index){
|
||||
ctx.sq1_audio_buffer[(ctx.sq1_write_index++)%4096] = square_sample[ctx.sq1_duty][ctx.sq1_sample];
|
||||
} else {
|
||||
printf("buffer overflow\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void audio_tick(){
|
||||
u32 prev_ticks = ticks;
|
||||
|
@ -44,7 +44,7 @@ void timer_tick() {
|
||||
}
|
||||
|
||||
if((prev_div & (1 << 1)) && (!(ctx.div & (1 << 1)))){
|
||||
//audio_period_tick();
|
||||
audio_period_tick();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user