#58962 - SittingDuck - Thu Oct 27, 2005 5:07 pm
What's wrong with this?
It works OK apart from clicks and crackles. I took the frequency and the buffer size from the list of perfect buffer sizes at http://deku.gbadev.org/program/sound1.html so I don't see how it's still clicking. The source data, testTone, is 44160 samples long so that it is divisible by the buffer size. I have put a sample of the sound output here.
Compiler: DevKitArm
Emulator: VisualBoyAdvance
Code: |
#include "gba.h"
#include "dispcnt.h" #include "interrupt.h" #include "core_asm.h" void DoVBlank(void); extern s8 testTone[]; s8* pos; ///// AUDIO BUFFERS ///// // adjust this to make the sound sound just right! #define ABUFSIZE 736 s8 abuffers[2][ABUFSIZE]; u32 abuf = 0; int main(void){ pos = &testTone[0]; memcpy32(&abuffers, pos, (ABUFSIZE*2)); pos += (ABUFSIZE*2); // set interrupt routine IntrTable[0] = DoVBlank; // enable video mode 4 REG_DISPCNT = DCNT_MODE_4 | DCNT_BG2_ENABLE; memset16(MEM_VRAM, 0xFFFF, 240*160); PALETTE256[255] = 0x1F; // initialize interrupt service routine int_init; // enable all interrupts REG_IME = 1; // request VBlank interrupt REG_IE |= INT_VBLANK; REG_DISPSTAT |= DSTAT_INT_V; // set up sound output REG_SOUNDCNT_L = 0; REG_SOUNDCNT_H = SNDCH_VOLUME_3 | SNDCH_A2LEFT | SNDCH_A2RIGHT; REG_SOUNDCNT_X = SNDCX_ON; // set up DMA REG_DMA1SAD = (u32) &abuffers[0]; REG_DMA1DAD = (u32) ®_SGFIFOA; REG_DMA1CNT_L = 0; REG_DMA1CNT_H = DMA_DST_FIXED | DMA_SRC_INC | DMA_REPEAT | DMA_WORD | DMA_START_FIFO | DMA_ENABLE; // set up timer for 44.1kHz REG_TM0CNT_L = 0xFE82; // is this right? REG_TM0CNT_H = TM_CYCLES_1 | TM_ENABLE; while(1); return 0; } void DoVBlank(void){ void* buffer; u16 old; // we have to turn the DMA off momentarily for the source address // change to take effect. Store the original settings first and // put them back afterwards. if(abuf == 1){ old = REG_DMA1CNT_H; // store old DMA settings REG_DMA1CNT_H = 0; // stop DMA REG_DMA1SAD = (u32) &abuffers[0]; // set DMA source NOP; REG_DMA1CNT_H = old; // start DMA abuf = 0; buffer = &abuffers[1]; } else { abuf = 1; buffer = &abuffers[0]; } memcpy32(buffer, pos, ABUFSIZE); // refill buffer pos += ABUFSIZE; if(((u32)pos-(u32)&testTone) > 44100) pos = (s8*)&testTone; PALETTE256[255]++; // debug purposes - shows it's running } |
It works OK apart from clicks and crackles. I took the frequency and the buffer size from the list of perfect buffer sizes at http://deku.gbadev.org/program/sound1.html so I don't see how it's still clicking. The source data, testTone, is 44160 samples long so that it is divisible by the buffer size. I have put a sample of the sound output here.
Compiler: DevKitArm
Emulator: VisualBoyAdvance