Microchip® Advanced Software Framework

sound.h File Reference

Sound sample and playback driver header file.

Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.

#include <tc.h>
#include <conf_example.h>

Macros

#define SOUND_SAMPLE_RATE   22050UL
 Rate at which DA conversions should be done. More...
 

Functions

void sound_init (void)
 Initialize the DAC, timer/counter and event system. More...
 
bool sound_is_playing (void)
 Convenience function to check if a sound is currently playing. More...
 
void sound_play_note (uint8_t button)
 Set sinewave frequencies to generate. More...
 
void sound_play_sample (uint8_t button)
 Starts playing samples when button status is sent. More...
 
static void sound_start_timer (void)
 Start the timer/counter used for sound output. More...
 
static void sound_stop_timer (void)
 Stop the timer/counter used for sound output. More...
 

#define SOUND_SAMPLE_RATE   22050UL

Rate at which DA conversions should be done.

Referenced by sound_init().

bool sound_is_playing ( void  )

Convenience function to check if a sound is currently playing.

Return values
trueSound is currently being played back.
falseSound is not being played back.

References snd_cnt, and snd_len.

Referenced by play_game(), and sound_play_sample().

void sound_play_note ( uint8_t  buttons)

Set sinewave frequencies to generate.

Sets the frequencies of notes to generate based on currently pressed buttons (each button corresponds to a unique note).

Note
This updates the global variable sinefreqs, and will start or stop the sound timer if some or no buttons are pressed, respectively.
Parameters
buttonsMask of buttons which are pressed.

References sinefreqs, sound_get_frequency_from_note(), sound_start_timer(), and sound_stop_timer().

Referenced by play_game(), and record_buttons().

void sound_play_sample ( uint8_t  buttons)

Starts playing samples when button status is sent.

Loads information about the sound to play starts the sound timer which triggers conversions and updates of sample value in DAC, at a rate of SOUND_SAMPLE_RATE times per second.

Note
This modifies the global variables snd_cnt, snd_len snd_samplewidth, and starts the sound timer.

See get_far.h for usage of GET_FAR_ADDRESS(symbol).

Parameters
buttonsMask of buttons which are pressed.

References GET_FAR_ADDRESS, prog_snd_ptr, snd_cnt, snd_len, snd_samplewidth, sound_is_playing(), and sound_start_timer().

Referenced by play_game(), and record_buttons().

static void sound_start_timer ( void  )
inlinestatic

Start the timer/counter used for sound output.

This will start the timer/counter which is used to:

  • generate events which trigger DAC conversions
  • trigger interrupt which causes loading of next sample to DAC

sound_init should be called before this function.

References tc_write_clock_source().

Referenced by sound_play_note(), and sound_play_sample().

static void sound_stop_timer ( void  )
inlinestatic

Stop the timer/counter used for sound output.

This will stop the timer/counter which is used for sound output, effectively causing playback to halt.

References tc_write_clock_source().

Referenced by play_game(), sample_callback(), and sound_play_note().