gameboy advance programming sound. sound basics the digital process a / d fs d / a sound pressure...

37
GameBoy Advance Programming Sound

Upload: christal-strickland

Post on 03-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

GameBoy AdvanceProgramming

Sound

Page 2: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Sound Basics

Page 3: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

The Digital Process

A / D

Fs

D / A

Sound pressure

fluctuations

Sound pressure

fluctuations

Analog voltage

Digital voltage

Sampling frequency

Sampled signal

Sampled signal

Digital voltage

Analog voltage

Page 4: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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

Page 5: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Sound

Step by Step

Page 6: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Getting the Sound

Obtain an 8-bit .wav file Convert to .bin using wav2gba.exe

wav2gba <input.wav> <output.bin>

Convert .bin to .c using bin2c.exebin2c <input.bin> <output.c>

Add the c file to your project Will be unsigned chars...

Don't forget to extern it in your main file

Page 7: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Sound Devices are Sensitive

Sensitive Sound Device

(DAC etc.)

FIFO QUEUE

FIFO QUEUE

FIFO QUEUE

FIFO QUEUE

TIMER

We tell the sound deviceto play a sample eachtime the timer times out.

We tell the sound deviceto play a sample eachtime the timer times out.

Page 8: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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)

Page 9: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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.

Page 10: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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?

Page 11: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling
Page 12: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

DMA to the Rescue

• The DMA can be ordered to automatically keep the FIFO full!!!!

Page 13: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Sound Devices are Sensitive

Sensitive Sound Device

(DAC etc.)

FIFO QUEUE

FIFO QUEUE

FIFO QUEUE

FIFO QUEUE

TIMER

YOURDATA

DMA

Page 14: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Master Sound Control

#define REG_SOUNDCNT_X *(u32*)0x04000084

#define SND_ENABLED 0x00000080

Page 15: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Sound Control Registers

#define REG_SOUNDCNT_L *(u16*)0x04000080

#define REG_SOUNDCNT_H *(u16*)0x04000082

THEONE

Page 16: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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

Page 17: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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

Page 18: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

REG_SOUNDCNT_H

BitsFunction0-1 Output sound ratio for channels 1 - 4 (00 = 25%, 01 = 50%, 10 = 100%)2 Direct Sound A output ratio (0 = 50%, 1 = 100%)3 Direct Sound B output ratio (0 = 50%, 1 = 100%)4-7 Unused8 Enable Direct Sound A output to right speaker (0 = No, 1 = Yes)9 Enable Direct Sound A output to left speaker (0 = No, 1 = Yes)10 Direct Sound A sampling rate Timer (0 = Timer 0, 1 = Timer 1)11 Direct Sound A FIFO reset (write a 1 here to reset the FIFO)12 Enable Direct Sound B output to right speaker (0 = No, 1 = Yes)13 Enable Direct Sound B output to left speaker (0 = No, 1 = Yes)14 Direct Sound B sampling rate Timer (0 = Timer 0, 1 = Timer 1)15 Direct Sound B FIFO reset (write a 1 here to reset the FIFO)

Page 19: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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

Page 20: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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

Page 21: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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

Page 22: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Timing is Everything/* -------------------------------------

* Timer interval

*

* 2^24/11025 = 16777216/11025 ~= 1522

*

*--------------------------------------*/

#define TIMER_INTERVAL (0xFFFF - 1522)

Page 23: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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;

Page 24: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Sound Controls

// we don’t want to mess with sound channels 1-4

REG_SOUNDCNT_L = 0;

Page 25: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Set Timer and Start it

REG_TM0D = TIMER_INTERVAL; REG_TM0CNT = TIMER_ENABLED;

Page 26: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Record start time

• Assume we have an interrupt handler that is counting vblanks for us and storing them in vblankcount

start = vblankcount;

Page 27: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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;

Page 28: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Now the Bad News

• Word count in the DMA control register does nothing

• GBA will play its entire memory!

Page 29: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Stopping

• Use a timer and when it overflows turn off sound

• Count vblanks and when you reach the limit turn off the sound.

Page 30: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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

Page 31: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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;

}

Page 32: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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

Page 33: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

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

Page 34: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Sound Checklist

• Set Timer and Start it

REG_TM0D = TIMER_INTERVAL; REG_TM0CNT = TIMER_ENABLED;

• Record the start time

Page 35: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Sound Checklist

• Start DMAREG_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

Page 36: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling

Questions?

Page 37: GameBoy Advance Programming Sound. Sound Basics The Digital Process A / D Fs D / A Sound pressure fluctuations Analog voltage Digital voltage Sampling