Download presentation
Presentation is loading. Please wait.
Published byChristal Strickland Modified over 9 years ago
1
GameBoy Advance Programming Sound
2
Sound Basics
3
The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling frequency Sampled signal Digital voltage Analog voltage
4
GBA Sound Four legacy sound effect channels –We won't use these Dual digital channels –Direct Sound A –Direct Sound B –Each has an 8-bit digital-to-analog converters (DACs) Play any sounds you want –Well, at least within the capabilities of 8 bit sound
5
Sound Step by Step
6
Getting the Sound Obtain an 8-bit.wav file Convert to.bin using wav2gba.exe wav2gba Convert.bin to.c using bin2c.exe bin2c Add the c file to your project Will be unsigned chars... Don't forget to extern it in your main file
7
Sound Devices are Sensitive Sensitive Sound Device (DAC etc.) FIFO QUEUE TIMER We tell the sound device to play a sample each time the timer times out. We tell the sound device to play a sample each time the timer times out.
8
Calculating Timer Timing Set up a timer to send samples from FIFO to the sound chip at the right frequency –Bit 10: Sound A sampling rate Timer –Bit 14: Sound B sampling rate Timer –0 or 1 in each bit for Timer 0 or 1 Figure out clock cycles per sample –2^24 / soundSamplingRate = cycles / sample Set up a timer to overflow after that many cycles –Initialize timer to value of 65536 – (# of cycles)
9
Example At 16.7 MHz, the CPU has exactly 16777216 (2^24) cycles per second To get the number of cycles per sample, divide 16777216 by the sampling rate. Suppose you want to play back a sample at one quarter CD Quality: 11.025 kHz –16777216 / 11025 = 1521.74 –So, set a timer to send a sample to the sound chip every 1522 CPU cycles. –That’s 1522 cycles per sample.
10
How do we keep the FIFO full? We could have an interrupt tell us when it needed another sample and put it in ourselves Sounds simple doesn't it?
12
DMA to the Rescue The DMA can be ordered to automatically keep the FIFO full!!!!
13
Sound Devices are Sensitive Sensitive Sound Device (DAC etc.) FIFO QUEUE TIMER YOUR DATA DMA
14
Master Sound Control #define REG_SOUNDCNT_X *(u32*)0x04000084 #define SND_ENABLED 0x00000080
15
Sound Control Registers #define REG_SOUNDCNT_L *(u16*)0x04000080 #define REG_SOUNDCNT_H *(u16*)0x04000082 THE ONE
16
Flags #define SND_ENABLED 0x00000080 #define SND_OUTPUT_RATIO_25 0x0000 #define SND_OUTPUT_RATIO_50 0x0001 #define SND_OUTPUT_RATIO_100 0x0002 #define DSA_OUTPUT_RATIO_50 0x0000 #define DSA_OUTPUT_RATIO_100 0x0004 #define DSA_OUTPUT_TO_RIGHT 0x0100 #define DSA_OUTPUT_TO_LEFT 0x0200 #define DSA_OUTPUT_TO_BOTH 0x0300
17
Flags (Continued) #define DSA_TIMER0 0x0000 #define DSA_TIMER1 0x0400 #define DSA_FIFO_RESET 0x0800 #define DSB_OUTPUT_RATIO_50 0x0000 #define DSB_OUTPUT_RATIO_100 0x0008 #define DSB_OUTPUT_TO_RIGHT 0x1000 #define DSB_OUTPUT_TO_LEFT 0x2000 #define DSB_OUTPUT_TO_BOTH 0x3000 #define DSB_TIMER0 0x0000 #define DSB_TIMER1 0x4000 #define DSB_FIFO_RESET 0x8000
18
REG_SOUNDCNT_H BitsFunction 0-1Output sound ratio for channels 1 - 4 (00 = 25%, 01 = 50%, 10 = 100%) 2Direct Sound A output ratio (0 = 50%, 1 = 100%) 3Direct Sound B output ratio (0 = 50%, 1 = 100%) 4-7Unused 8Enable Direct Sound A output to right speaker (0 = No, 1 = Yes) 9Enable Direct Sound A output to left speaker (0 = No, 1 = Yes) 10Direct Sound A sampling rate Timer (0 = Timer 0, 1 = Timer 1) 11Direct Sound A FIFO reset (write a 1 here to reset the FIFO) 12Enable Direct Sound B output to right speaker (0 = No, 1 = Yes) 13Enable Direct Sound B output to left speaker (0 = No, 1 = Yes) 14Direct Sound B sampling rate Timer (0 = Timer 0, 1 = Timer 1) 15Direct Sound B FIFO reset (write a 1 here to reset the FIFO)
19
DMA channel 1 register definitions #define REG_DMA1SAD *(u32*)0x40000BC // src addr #define REG_DMA1DAD *(u32*)0x40000C0 // dest addr #define REG_DMA1CNT *(u32*)0x40000C4 // cont reg
20
DMA flags #define WORD_DMA 0x04000000 #define HALF_WORD_DMA 0x00000000 #define ENABLE_DMA 0x80000000 #define START_ON_FIFO_EMPTY 0x30000000 #define DMA_REPEAT 0x02000000 #define DEST_REG_SAME 0x00400000
21
Timer and FIFO // Timer 0 register definitions #define REG_TM0D *(u16 *)0x4000100 #define REG_TM0CNT *(u16 *)0x4000102 // Timer flags #define TIMER_ENABLED 0x0080 // FIFO address defines #define REG_FIFO_A 0x040000A0 #define REG_FIFO_B 0x040000A4
22
Timing is Everything /* ------------------------------------- * Timer interval * * 2^24/11025 = 16777216/11025 ~= 1522 * *--------------------------------------*/ #define TIMER_INTERVAL (0xFFFF - 1522)
23
Sound Controls // Enable sound (Master control) REG_SOUNDCNT_X = SND_ENABLED; // enable and reset Direct Sound channel A, at full volume, // using Timer 0 to determine frequency REG_SOUNDCNT_H = SND_OUTPUT_RATIO_100 | DSA_OUTPUT_RATIO_100 | DSA_OUTPUT_TO_BOTH | DSA_TIMER0 | DSA_FIFO_RESET;
24
Sound Controls // we don’t want to mess with sound channels 1-4 REG_SOUNDCNT_L = 0;
25
Set Timer and Start it REG_TM0D = TIMER_INTERVAL; REG_TM0CNT = TIMER_ENABLED;
26
Record start time Assume we have an interrupt handler that is counting vblanks for us and storing them in vblankcount start = vblankcount;
27
Start the DMA Transfer REG_DMA1SAD = (u32)(ifyou); REG_DMA1DAD = (u32)REG_FIFO_A; REG_DMA1CNT = ENABLE_DMA | START_ON_FIFO_EMPTY | WORD_DMA | DMA_REPEAT;
28
Now the Bad News Word count in the DMA control register does nothing GBA will play its entire memory!
29
Stopping Use a timer and when it overflows turn off sound Count vblanks and when you reach the limit turn off the sound.
30
Play for How Long? vblanks occur 59.727 times per second. So, let's say you want the sample to play for 7.4 seconds You want 59.727x7.4 ~= 442 vblanks Note: Duration of sound is # Samples/Sample rate
31
void intHndlr(void) { REG_IME = 0; // Disable interrupts if(REG_IF == INT_VBLANK) { vblankcount++; if( (vblankcount - start) > 442) { REG_TM0CNT = 0; REG_DMA1CNT = 0; } REG_IF = REG_IF; REG_IME = 1; }
32
Sound Checklist Obtain (by hook or by crook) an 8 bit.wav sample. Convert using wav2gba Convert using bin2c Include the sound sample data in your program
33
Sound Checklist Enable Master Sound Control Set Sound Control High Register –SND_OUTPUT_RATIO_100 –DSA_OUTPUT_RATIO_100 –DSA_OUTPUT_TO_BOTH –DSA_TIMER0 –DSA_FIFO_RESET
34
Sound Checklist Set Timer and Start it REG_TM0D = TIMER_INTERVAL; REG_TM0CNT = TIMER_ENABLED; Record the start time
35
Sound Checklist Start DMA REG_DMA1SAD = (u32)(ifyou); REG_DMA1DAD = (u32)REG_FIFO_A; REG_DMA1CNT = ENABLE_DMA | START_ON_FIFO_EMPTY | WORD_DMA | DMA_REPEAT; Use an interrupt to stop timer and DMA to stop sound
36
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.