FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY > Class Template Reference

This API defines a player of melodies, defined as a sequence of tones and durations. More...

#include <fastarduino/devices/tone_player.h>

Inheritance diagram for devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >:
Collaboration diagram for devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >:

Public Types

using GENERATOR = typename BASE::GENERATOR
 The type of ToneGenerator to use as constructor's argument. More...
 
using TONE_PLAY = typename BASE::TONE_PLAY
 The type that holds unit of information of a melody. More...
 

Public Member Functions

 TonePlayer (GENERATOR &tone_generator)
 Create a new synchronous tone player, based on an existing ToneGenerator. More...
 
void play_sram (const TONE_PLAY *melody, const Beat &beat)
 Play a melody, defined by a sequence of TONE_PLAYs, stored in SRAM. More...
 
void play_eeprom (const TONE_PLAY *melody, const Beat &beat)
 Play a melody, defined by a sequence of TONE_PLAYs, stored in EEPROM. More...
 
void play_flash (const TONE_PLAY *melody, const Beat &beat)
 Play a melody, defined by a sequence of TONE_PLAYs, stored in Flash. More...
 
void stop ()
 Stop playing current melody (if any). More...
 
bool is_playing () const
 Tell if a melody is currently playing. More...
 

Additional Inherited Members

- Protected Types inherited from devices::audio::AbstractTonePlayer< NTIMER, OUTPUT, TONEPLAY >
using TONE_PLAY = TONEPLAY
 The type that holds unit of information of a melody. More...
 
using GENERATOR = ToneGenerator< NTIMER, OUTPUT >
 The type of ToneGenerator to use as constructor's argument. More...
 
- Protected Member Functions inherited from devices::audio::AbstractTonePlayer< NTIMER, OUTPUT, TONEPLAY >
 AbstractTonePlayer (GENERATOR &tone_generator)
 Create a new tone player, based on an existing ToneGenerator. More...
 
void set_min_duration (uint16_t min_duration)
 Set the duration, in milliseconds, of a 32nd note. More...
 
uint16_t get_min_duration () const
 Get the duration, in milliseconds, of a 32nd note. More...
 
void prepare_sram (const TONE_PLAY *melody)
 Prepare playing of melody, which should be stored in SRAM. More...
 
void prepare_eeprom (const TONE_PLAY *melody)
 Prepare playing of melody, which should be stored in EEPROM. More...
 
void prepare_flash (const TONE_PLAY *melody)
 Prepare playing of melody, which should be stored in Flash. More...
 
uint16_t start_next_note ()
 Ask this player to start playing the next note of the melody. More...
 
uint16_t stop_current_note ()
 Ask this player to stop playing the current note of the melody. More...
 
bool is_finished () const
 Indicate if the currently played melody is finished. More...
 

Detailed Description

template<board::Timer NTIMER, board::PWMPin OUTPUT, typename TONEPLAY = QTonePlay<NTIMER, OUTPUT>>
class devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >

This API defines a player of melodies, defined as a sequence of tones and durations.

This player is synchronous, i.e. when asking it to play a melody, the called method will not return until the melody is finished playing (or if stop() has been called, e.g. by an ISR).

Melodies are defined as sequence of unit information, which can be either:

  • TonePlays: easy to write in source code but not efficient in size of generated code
  • QTonePlays: requires more effort in source code, but reduces generated code size Which types is used is defined as the TONEPLAY template parameter.

With this API, played melodies can be stored on 3 possible locations:

  • in SRAM: this is useful when you get the melody from another support e.g. an external flash device
  • in Flash: this is the mostly used way as flash s the more abundant storage in AVR MCU
  • in EEPROM: this can be useful for short melodies, when you do not want to waste precious SRAM and Flash

Each API has 3 distinct methods, one for each storage strategy.

Template Parameters
NTIMERthe AVR timer to use for the underlying Timer
OUTPUTthe board::PWMPin connected to the buzzer; this must be the pin OCnA, where n is the AVR Timer number
TONEPLAYthe type used to store melody data, QTonePlay by default
See also
TonePlay
QTonePlay
AsyncTonePlayer

Definition at line 712 of file tone_player.h.

Member Typedef Documentation

◆ GENERATOR

template<board::Timer NTIMER, board::PWMPin OUTPUT, typename TONEPLAY = QTonePlay<NTIMER, OUTPUT>>
using devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >::GENERATOR = typename BASE::GENERATOR

The type of ToneGenerator to use as constructor's argument.

Definition at line 718 of file tone_player.h.

◆ TONE_PLAY

template<board::Timer NTIMER, board::PWMPin OUTPUT, typename TONEPLAY = QTonePlay<NTIMER, OUTPUT>>
using devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >::TONE_PLAY = typename BASE::TONE_PLAY

The type that holds unit of information of a melody.

Definition at line 720 of file tone_player.h.

Constructor & Destructor Documentation

◆ TonePlayer()

template<board::Timer NTIMER, board::PWMPin OUTPUT, typename TONEPLAY = QTonePlay<NTIMER, OUTPUT>>
devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >::TonePlayer ( GENERATOR tone_generator)
inlineexplicit

Create a new synchronous tone player, based on an existing ToneGenerator.

Parameters
tone_generatorthe ToneGenerator used to actually produce tones.

Definition at line 727 of file tone_player.h.

Member Function Documentation

◆ play_sram()

template<board::Timer NTIMER, board::PWMPin OUTPUT, typename TONEPLAY = QTonePlay<NTIMER, OUTPUT>>
void devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >::play_sram ( const TONE_PLAY melody,
const Beat beat 
)
inline

Play a melody, defined by a sequence of TONE_PLAYs, stored in SRAM.

This method is blocking: it will return only when the melody is finished playing.

Parameters
melodya pointer, in SRAM, to the sequence of TONE_PLAY to be played; the sequence MUST finish with a SpecialTone::END.
beatthe tempo (beats per minute) at which the melody shall be played; one beat is the duration of a quarter note.
See also
play_eeprom()
play_flash()
stop()

Definition at line 741 of file tone_player.h.

◆ play_eeprom()

template<board::Timer NTIMER, board::PWMPin OUTPUT, typename TONEPLAY = QTonePlay<NTIMER, OUTPUT>>
void devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >::play_eeprom ( const TONE_PLAY melody,
const Beat beat 
)
inline

Play a melody, defined by a sequence of TONE_PLAYs, stored in EEPROM.

This method is blocking: it will return only when the melody is finished playing.

Parameters
melodya pointer, in EEPROM, to the sequence of TONE_PLAY to be played; the sequence MUST finish with a SpecialTone::END.
beatthe tempo (beats per minute) at which the melody shall be played; one beat is the duration of a quarter note.
See also
play_sram()
play_flash()
stop()

Definition at line 760 of file tone_player.h.

◆ play_flash()

template<board::Timer NTIMER, board::PWMPin OUTPUT, typename TONEPLAY = QTonePlay<NTIMER, OUTPUT>>
void devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >::play_flash ( const TONE_PLAY melody,
const Beat beat 
)
inline

Play a melody, defined by a sequence of TONE_PLAYs, stored in Flash.

This method is blocking: it will return only when the melody is finished playing.

Parameters
melodya pointer, in Flash, to the sequence of TONE_PLAY to be played; the sequence MUST finish with a SpecialTone::END.
beatthe tempo (beats per minute) at which the melody shall be played; one beat is the duration of a quarter note.
See also
play_eeprom()
play_sram()
stop()

Definition at line 779 of file tone_player.h.

◆ stop()

template<board::Timer NTIMER, board::PWMPin OUTPUT, typename TONEPLAY = QTonePlay<NTIMER, OUTPUT>>
void devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >::stop ( )
inline

Stop playing current melody (if any).

Effect is not immediate but will stop at the end of the current tone.

Definition at line 790 of file tone_player.h.

◆ is_playing()

template<board::Timer NTIMER, board::PWMPin OUTPUT, typename TONEPLAY = QTonePlay<NTIMER, OUTPUT>>
bool devices::audio::TonePlayer< NTIMER, OUTPUT, TONEPLAY >::is_playing ( ) const
inline

Tell if a melody is currently playing.

Definition at line 798 of file tone_player.h.


The documentation for this class was generated from the following file: