libnds
Enumerations | Functions
sound.h File Reference

A simple sound playback library for the DS. More...

#include <nds/ndstypes.h>

Enumerations

enum  DutyCycle {
  DutyCycle_0 = 7 ,
  DutyCycle_12 = 0 ,
  DutyCycle_25 = 1 ,
  DutyCycle_37 = 2 ,
  DutyCycle_50 = 3 ,
  DutyCycle_62 = 4 ,
  DutyCycle_75 = 5 ,
  DutyCycle_87 = 6
}
 PSG Duty cycles used by the PSG hardware. More...
 
enum  MicFormat {
  MicFormat_8Bit = 1 ,
  MicFormat_12Bit = 0
}
 Microphone recording formats DS. More...
 
enum  SoundFormat {
  SoundFormat_16Bit = 1 ,
  SoundFormat_8Bit = 0 ,
  SoundFormat_PSG = 3 ,
  SoundFormat_ADPCM = 2
}
 Sound formats used by the DS. More...
 

Functions

void soundDisable (void)
 Disables Sound on the DS.
 
void soundEnable (void)
 Enables Sound on the DS. More...
 
void soundExtSetFrequency (unsigned int freq_khz)
 Set extended sound hardware frequency. More...
 
void soundExtSetRatio (unsigned int ratio)
 Set the DSP/ARM volume ratio of the speakers output. More...
 
void soundKill (int soundId)
 Stops the sound specified by soundId and frees any resources allocated. More...
 
void soundMicOff (void)
 Stops the microphone from recording.
 
void soundMicPowerOff (void)
 Powers off the microphone after powering it on with soundMicPowerOn().
 
void soundMicPowerOn (void)
 Powers on the microphone so that it can be used by the DSP. More...
 
int soundMicRecord (void *buffer, u32 bufferLength, MicFormat format, int freq, MicCallback callback)
 Starts a microphone recording to a double buffer specified by buffer. More...
 
void soundPause (int soundId)
 Pause the sound specified by soundId. More...
 
int soundPlayNoise (u16 freq, u8 volume, u8 pan)
 Plays white noise with the specified parameters. More...
 
int soundPlayPSG (DutyCycle cycle, u16 freq, u8 volume, u8 pan)
 Pause a tone with the specified properties. More...
 
int soundPlaySample (const void *data, SoundFormat format, u32 dataSize, u16 freq, u8 volume, u8 pan, bool loop, u16 loopPoint)
 Plays a sound in the specified format at the specified frequency. More...
 
void soundResume (int soundId)
 Resumes a paused sound. More...
 
void soundSetFreq (int soundId, u16 freq)
 Sets the sound frequency. More...
 
void soundSetPan (int soundId, u8 pan)
 Sets the sound panning. More...
 
void soundSetVolume (int soundId, u8 volume)
 Sets the sound volume. More...
 
void soundSetWaveDuty (int soundId, DutyCycle cycle)
 Sets the Wave Duty of a PSG sound. More...
 

Detailed Description

A simple sound playback library for the DS.

Provides functionality for starting and stopping sound effects from the ARM9 side as well as access to PSG and noise hardware. Maxmod should be used in most music and sound effect situations.

Enumeration Type Documentation

◆ DutyCycle

enum DutyCycle

PSG Duty cycles used by the PSG hardware.

Enumerator
DutyCycle_0 

0.0% duty cycle

DutyCycle_12 

12.5% duty cycle

DutyCycle_25 

25.0% duty cycle

DutyCycle_37 

37.5% duty cycle

DutyCycle_50 

50.0% duty cycle

DutyCycle_62 

62.5% duty cycle

DutyCycle_75 

75.0% duty cycle

DutyCycle_87 

87.5% duty cycle

◆ MicFormat

enum MicFormat

Microphone recording formats DS.

Enumerator
MicFormat_8Bit 

8-bit PCM

MicFormat_12Bit 

12-bit PCM

◆ SoundFormat

Sound formats used by the DS.

Enumerator
SoundFormat_16Bit 

16-bit PCM

SoundFormat_8Bit 

8-bit PCM

SoundFormat_PSG 

PSG (Programmable Sound Generator)

SoundFormat_ADPCM 

IMA ADPCM compressed audio.

Function Documentation

◆ soundEnable()

void soundEnable ( void  )

Enables Sound on the DS.

It should be called prior to attempting sound playback.

◆ soundExtSetFrequency()

void soundExtSetFrequency ( unsigned int  freq_khz)

Set extended sound hardware frequency.

Parameters
freq_khzFrequency in KHz. The default is 32, but 47 is allowed too.

◆ soundExtSetRatio()

void soundExtSetRatio ( unsigned int  ratio)

Set the DSP/ARM volume ratio of the speakers output.

Parameters
ratioValue from 0 to 8 where 0 is 100% DSP, 8 is 100% ARM, 4 is 50% DSP and 50% ARM.

◆ soundKill()

void soundKill ( int  soundId)

Stops the sound specified by soundId and frees any resources allocated.

Parameters
soundIdThe sound ID returned by play sound.

◆ soundMicPowerOn()

void soundMicPowerOn ( void  )

Powers on the microphone so that it can be used by the DSP.

It won't record anything, it will simply remain powered on. You can power it off with soundMicPowerOff().

This isn't required for soundMicRecord(), this is only required for the DSP. Don't use it for anything else

◆ soundMicRecord()

int soundMicRecord ( void *  buffer,
u32  bufferLength,
MicFormat  format,
int  freq,
MicCallback  callback 
)

Starts a microphone recording to a double buffer specified by buffer.

Note: The microphone uses timer 1 in the ARM7.

Parameters
bufferA pointer to the start of the double buffer.
bufferLengthThe length of the buffer in bytes (both halfs of the double buffer).
formatMicrophone can record in 8 or 12 bit format. 12 bit is shifted up to 16 bit PCM.
freqThe sample frequency.
callbackCalled every time the buffer is full or half full.
Returns
Returns non zero for success.

◆ soundPause()

void soundPause ( int  soundId)

Pause the sound specified by soundId.

Parameters
soundIdThe sound ID returned by play sound.

◆ soundPlayNoise()

int soundPlayNoise ( u16  freq,
u8  volume,
u8  pan 
)

Plays white noise with the specified parameters.

Parameters
freqThe frequency in Hz of the sample.
volumeThe channel volume. 0 to 127 (min to max).
panThe channel pan 0 to 127 (left to right with 64 being centered).
Returns
An integer id coresponding to the channel of playback. This value can be used to pause, resume, or kill the sound as well as adjust volume, pan, and frequency.

◆ soundPlayPSG()

int soundPlayPSG ( DutyCycle  cycle,
u16  freq,
u8  volume,
u8  pan 
)

Pause a tone with the specified properties.

Parameters
cycleThe DutyCycle of the sound wave.
freqThe frequency in Hz of the sample.
volumeThe channel volume. 0 to 127 (min to max)
panThe channel pan 0 to 127 (left to right with 64 being centered).
Returns
An integer id coresponding to the channel of playback. This value can be used to pause, resume, or kill the sound as well as adjust volume, pan, and frequency.

◆ soundPlaySample()

int soundPlaySample ( const void *  data,
SoundFormat  format,
u32  dataSize,
u16  freq,
u8  volume,
u8  pan,
bool  loop,
u16  loopPoint 
)

Plays a sound in the specified format at the specified frequency.

Parameters
dataA pointer to the sound data.
formatThe format of the data (only 16-bit and 8-bit pcm and ADPCM formats are supported by this function).
dataSizeThe size in bytes of the sound data.
freqThe frequency in Hz of the sample.
volumeThe channel volume. 0 to 127 (min to max).
panThe channel pan 0 to 127 (left to right with 64 being centered).
loopIf true, the sample will loop playing once then repeating starting at the offset stored in loopPoint.
loopPointThe offset for the sample loop to restart when repeating.
Returns
An integer id coresponding to the channel of playback. This value can be used to pause, resume, or kill the sound as well as adjust volume, pan, and frequency

◆ soundResume()

void soundResume ( int  soundId)

Resumes a paused sound.

Parameters
soundIdThe sound ID returned by play sound.

◆ soundSetFreq()

void soundSetFreq ( int  soundId,
u16  freq 
)

Sets the sound frequency.

Parameters
soundIdThe sound ID returned by play sound.
freqThe frequency in Hz.

◆ soundSetPan()

void soundSetPan ( int  soundId,
u8  pan 
)

Sets the sound panning.

Parameters
soundIdThe sound ID returned by play sound.
panThe new pan value (0 to 127 left to right, 64 = center).

◆ soundSetVolume()

void soundSetVolume ( int  soundId,
u8  volume 
)

Sets the sound volume.

Parameters
soundIdThe sound ID returned by play sound.
volumeThe new volume (0 to 127 min to max).

◆ soundSetWaveDuty()

void soundSetWaveDuty ( int  soundId,
DutyCycle  cycle 
)

Sets the Wave Duty of a PSG sound.

Parameters
soundIdThe sound ID returned by play sound.
cycleThe DutyCycle of the sound wave.