M5Unified
m5::Speaker_Class Class Reference

#include <Speaker_Class.hpp>

Collaboration diagram for m5::Speaker_Class:

Classes

struct  channel_info_t
 
struct  wav_info_t
 

Public Member Functions

virtual ~Speaker_Class (void)
 
speaker_config_t config (void) const
 
void config (const speaker_config_t &cfg)
 
bool begin (void)
 
void end (void)
 
bool isRunning (void) const
 
bool isEnabled (void) const
 
bool isPlaying (void) const volatile
 
size_t isPlaying (uint8_t channel) const volatile
 
size_t getPlayingChannels (void) const volatile
 
void setVolume (uint8_t master_volume)
 
uint8_t getVolume (void) const
 
void setAllChannelVolume (uint8_t volume)
 
void setChannelVolume (uint8_t channel, uint8_t volume)
 
uint8_t getChannelVolume (uint8_t channel) const
 
void stop (void)
 stop sound output. More...
 
void stop (uint8_t channel)
 
bool tone (float frequency, uint32_t duration, int channel, bool stop_current_sound, const uint8_t *raw_data, size_t array_len, bool stereo=false)
 
bool tone (float frequency, uint32_t duration=UINT32_MAX, int channel=-1, bool stop_current_sound=true)
 
bool playRaw (const int8_t *raw_data, size_t array_len, uint32_t sample_rate=44100, bool stereo=false, uint32_t repeat=1, int channel=-1, bool stop_current_sound=false)
 
bool playRAW (const int8_t *raw_data, size_t array_len, uint32_t sample_rate=44100, bool stereo=false, uint32_t repeat=1, int channel=-1, bool stop_current_sound=false)
 
bool playRaw (const uint8_t *raw_data, size_t array_len, uint32_t sample_rate=44100, bool stereo=false, uint32_t repeat=1, int channel=-1, bool stop_current_sound=false)
 
bool playRAW (const uint8_t *raw_data, size_t array_len, uint32_t sample_rate=44100, bool stereo=false, uint32_t repeat=1, int channel=-1, bool stop_current_sound=false)
 
bool playRaw (const int16_t *raw_data, size_t array_len, uint32_t sample_rate=44100, bool stereo=false, uint32_t repeat=1, int channel=-1, bool stop_current_sound=false)
 
bool playRAW (const int16_t *raw_data, size_t array_len, uint32_t sample_rate=44100, bool stereo=false, uint32_t repeat=1, int channel=-1, bool stop_current_sound=false)
 
bool playWav (const uint8_t *wav_data, size_t data_len=~0u, uint32_t repeat=1, int channel=-1, bool stop_current_sound=false)
 

Protected Member Functions

void setCallback (void *args, bool(*func)(void *, bool))
 
esp_err_t _setup_i2s (void)
 
bool _play_raw (const void *wav, size_t array_len, bool flg_16bit, bool flg_signed, float sample_rate, bool flg_stereo, uint32_t repeat_count, int channel, bool stop_current_sound, bool no_clear_index)
 
bool _set_next_wav (size_t ch, const wav_info_t &wav)
 

Static Protected Member Functions

static void spk_task (void *args)
 

Protected Attributes

channel_info_t _ch_info [sound_channel_max]
 
speaker_config_t _cfg
 
volatile uint8_t _master_volume = 64
 
bool(* _cb_set_enabled )(void *args, bool enabled) = nullptr
 
void * _cb_set_enabled_args = nullptr
 
volatile bool _task_running = false
 
std::atomic< uint16_t > _play_channel_bits = { 0 }
 
TaskHandle_t _task_handle = nullptr
 
volatile SemaphoreHandle_t _task_semaphore = nullptr
 

Static Protected Attributes

static constexpr const size_t sound_channel_max = 8
 
static const uint8_t _default_tone_wav [16] = { 177, 219, 246, 255, 246, 219, 177, 128, 79, 37, 10, 1, 10, 37, 79, 128 }
 

Detailed Description

Definition at line 73 of file Speaker_Class.hpp.

Constructor & Destructor Documentation

◆ ~Speaker_Class()

virtual m5::Speaker_Class::~Speaker_Class ( void  )
inlinevirtual

Definition at line 77 of file Speaker_Class.hpp.

Member Function Documentation

◆ _play_raw()

bool m5::Speaker_Class::_play_raw ( const void *  wav,
size_t  array_len,
bool  flg_16bit,
bool  flg_signed,
float  sample_rate,
bool  flg_stereo,
uint32_t  repeat_count,
int  channel,
bool  stop_current_sound,
bool  no_clear_index 
)
protected

Definition at line 798 of file Speaker_Class.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _set_next_wav()

bool m5::Speaker_Class::_set_next_wav ( size_t  ch,
const wav_info_t wav 
)
protected

Definition at line 773 of file Speaker_Class.cpp.

Here is the caller graph for this function:

◆ _setup_i2s()

esp_err_t m5::Speaker_Class::_setup_i2s ( void  )
protected

DACが使用できるのはI2Sポート0のみ。;

all pin set to I2S_PIN_NO_CHANGE

レジスタを操作してDACモードの設定を有効にする(I2S0のみ。I2S1はDAC,ADC非対応) ;

Definition at line 63 of file Speaker_Class.cpp.

Here is the caller graph for this function:

◆ begin()

bool m5::Speaker_Class::begin ( void  )

Definition at line 683 of file Speaker_Class.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ config() [1/2]

void m5::Speaker_Class::config ( const speaker_config_t cfg)
inline

Definition at line 80 of file Speaker_Class.hpp.

◆ config() [2/2]

speaker_config_t m5::Speaker_Class::config ( void  ) const
inline

Definition at line 79 of file Speaker_Class.hpp.

◆ end()

void m5::Speaker_Class::end ( void  )

Definition at line 719 of file Speaker_Class.cpp.

Here is the call graph for this function:

◆ getChannelVolume()

uint8_t m5::Speaker_Class::getChannelVolume ( uint8_t  channel) const
inline

gets the output volume of the sound for the specified virtual channel.

Parameters
channelvirtual channel number. (0~7)
Returns
channel volume.

Definition at line 130 of file Speaker_Class.hpp.

◆ getPlayingChannels()

size_t m5::Speaker_Class::getPlayingChannels ( void  ) const volatile
inline

Get the number of channels that are playing.

Returns
number of channels that are playing.

Definition at line 108 of file Speaker_Class.hpp.

◆ getVolume()

uint8_t m5::Speaker_Class::getVolume ( void  ) const
inline

gets the output master volume of the sound.

Returns
master volume.

Definition at line 116 of file Speaker_Class.hpp.

◆ isEnabled()

bool m5::Speaker_Class::isEnabled ( void  ) const
inline

Definition at line 88 of file Speaker_Class.hpp.

◆ isPlaying() [1/2]

size_t m5::Speaker_Class::isPlaying ( uint8_t  channel) const volatile
inline

now in playing or not.

Parameters
channelvirtual channel number. (0~7), (default = automatically selected)
Returns
0=not playing / 1=playing (There's room in the queue) / 2=playing (There's no room in the queue.)

Definition at line 104 of file Speaker_Class.hpp.

◆ isPlaying() [2/2]

bool m5::Speaker_Class::isPlaying ( void  ) const volatile
inline

now in playing or not.

Returns
false=not playing / true=playing

Definition at line 99 of file Speaker_Class.hpp.

◆ isRunning()

bool m5::Speaker_Class::isRunning ( void  ) const
inline

Definition at line 86 of file Speaker_Class.hpp.

◆ playRaw() [1/3]

bool m5::Speaker_Class::playRaw ( const int16_t *  raw_data,
size_t  array_len,
uint32_t  sample_rate = 44100,
bool  stereo = false,
uint32_t  repeat = 1,
int  channel = -1,
bool  stop_current_sound = false 
)
inline

play raw sound wave data. (for signed 16bit wav data)

Parameters
raw_datawave data.
array_lenNumber of data array elements.
sample_ratethe sampling rate (Hz) (default = 44100)
stereotrue=data is stereo / false=data is monaural.
repeatnumber of times played repeatedly. (default = 1)
channelvirtual channel number (If omitted, use an available channel.)
stop_current_soundtrue=start a new output without waiting for the current one to finish.
Attention
If you want to use the data generated at runtime, you can either have three buffers and use them in sequence, or have two buffers and use them alternately, then split them in half and call playRaw twice.
If noise is present in the output sounds, consider increasing the priority of the task that generates the data.

Definition at line 208 of file Speaker_Class.hpp.

Here is the call graph for this function:

◆ playRAW() [1/3]

bool m5::Speaker_Class::playRAW ( const int16_t *  raw_data,
size_t  array_len,
uint32_t  sample_rate = 44100,
bool  stereo = false,
uint32_t  repeat = 1,
int  channel = -1,
bool  stop_current_sound = false 
)
inline
Deprecated:
"playRAW" function has been renamed to "playRaw"

Definition at line 215 of file Speaker_Class.hpp.

Here is the call graph for this function:

◆ playRaw() [2/3]

bool m5::Speaker_Class::playRaw ( const int8_t *  raw_data,
size_t  array_len,
uint32_t  sample_rate = 44100,
bool  stereo = false,
uint32_t  repeat = 1,
int  channel = -1,
bool  stop_current_sound = false 
)
inline

play raw sound wave data. (for signed 8bit wav data)

Parameters
raw_datawave data.
array_lenNumber of data array elements.
sample_ratethe sampling rate (Hz) (default = 44100)
stereotrue=data is stereo / false=data is monaural.
repeatnumber of times played repeatedly. (default = 1)
channelvirtual channel number (If omitted, use an available channel.)
stop_current_soundtrue=start a new output without waiting for the current one to finish.
Attention
If you want to use the data generated at runtime, you can either have three buffers and use them in sequence, or have two buffers and use them alternately, then split them in half and call playRaw twice.
If noise is present in the output sounds, consider increasing the priority of the task that generates the data.

Definition at line 168 of file Speaker_Class.hpp.

Here is the call graph for this function:

◆ playRAW() [2/3]

bool m5::Speaker_Class::playRAW ( const int8_t *  raw_data,
size_t  array_len,
uint32_t  sample_rate = 44100,
bool  stereo = false,
uint32_t  repeat = 1,
int  channel = -1,
bool  stop_current_sound = false 
)
inline

Definition at line 173 of file Speaker_Class.hpp.

Here is the call graph for this function:

◆ playRaw() [3/3]

bool m5::Speaker_Class::playRaw ( const uint8_t *  raw_data,
size_t  array_len,
uint32_t  sample_rate = 44100,
bool  stereo = false,
uint32_t  repeat = 1,
int  channel = -1,
bool  stop_current_sound = false 
)
inline

play raw sound wave data. (for unsigned 8bit wav data)

Parameters
raw_datawave data.
array_lenNumber of data array elements.
sample_ratethe sampling rate (Hz) (default = 44100)
stereotrue=data is stereo / false=data is monaural.
repeatnumber of times played repeatedly. (default = 1)
channelvirtual channel number (If omitted, use an available channel.)
stop_current_soundtrue=start a new output without waiting for the current one to finish.
Attention
If you want to use the data generated at runtime, you can either have three buffers and use them in sequence, or have two buffers and use them alternately, then split them in half and call playRaw twice.
If noise is present in the output sounds, consider increasing the priority of the task that generates the data.

Definition at line 188 of file Speaker_Class.hpp.

Here is the call graph for this function:

◆ playRAW() [3/3]

bool m5::Speaker_Class::playRAW ( const uint8_t *  raw_data,
size_t  array_len,
uint32_t  sample_rate = 44100,
bool  stereo = false,
uint32_t  repeat = 1,
int  channel = -1,
bool  stop_current_sound = false 
)
inline

Definition at line 193 of file Speaker_Class.hpp.

Here is the call graph for this function:

◆ playWav()

bool m5::Speaker_Class::playWav ( const uint8_t *  wav_data,
size_t  data_len = ~0u,
uint32_t  repeat = 1,
int  channel = -1,
bool  stop_current_sound = false 
)

play WAV format sound data.

Parameters
wav_datawave data. (WAV header included)
repeatnumber of times played repeatedly. (default = 1)
channelvirtual channel number (If omitted, use an available channel.)
stop_current_soundtrue=start a new output without waiting for the current one to finish.

Definition at line 826 of file Speaker_Class.cpp.

Here is the call graph for this function:

◆ setAllChannelVolume()

void m5::Speaker_Class::setAllChannelVolume ( uint8_t  volume)
inline

sets the output volume of the sound for the all virtual channel.

Parameters
volumechannel volume (0~255)

Definition at line 120 of file Speaker_Class.hpp.

◆ setCallback()

void m5::Speaker_Class::setCallback ( void *  args,
bool(*)(void *, bool)  func 
)
inlineprotected

Definition at line 233 of file Speaker_Class.hpp.

◆ setChannelVolume()

void m5::Speaker_Class::setChannelVolume ( uint8_t  channel,
uint8_t  volume 
)
inline

sets the output volume of the sound for the specified virtual channel.

Parameters
channelvirtual channel number. (0~7)
volumechannel volume (0~255)

Definition at line 125 of file Speaker_Class.hpp.

◆ setVolume()

void m5::Speaker_Class::setVolume ( uint8_t  master_volume)
inline

sets the output master volume of the sound.

Parameters
master_volumemaster volume (0~255)

Definition at line 112 of file Speaker_Class.hpp.

◆ spk_task()

void m5::Speaker_Class::spk_task ( void *  args)
staticprotected

1サンプリング当たりの出力ビット数;

MCLKを使用しない場合、サンプリングレート誤差が少なくなるようにdiv_mを調整する;

実際に設定されたサンプリングレートの算出を行う;

liner_prevからliner_baseへの2サンプル間の線形補間;

DAC出力は cfg.dac_zero_levelが0に設定されている場合、振幅のオフセットを動的に変更する。; DAC出力が低いほどノイズ音が減るため、なるべくDAC出力を下げてノイズを低減することを目的とする。;

ブザー出力は 1bit ΔΣ方式。 I2Sデータ出力をブザーの駆動信号として利用する; 出力はモノラル限定だが、I2Sへはステレオ扱いで出力する。; (I2Sをモノラル設定にした場合は同じデータが2チャンネル分送信されてしまうため、敢えてステレオ扱いとしている);

Definition at line 188 of file Speaker_Class.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ stop() [1/2]

void m5::Speaker_Class::stop ( uint8_t  channel)

stop sound output for the specified virtual channel.

Parameters
channelvirtual channel number. (0~7)

Definition at line 749 of file Speaker_Class.cpp.

Here is the call graph for this function:

◆ stop() [2/2]

void m5::Speaker_Class::stop ( void  )

stop sound output.

Definition at line 738 of file Speaker_Class.cpp.

Here is the caller graph for this function:

◆ tone() [1/2]

bool m5::Speaker_Class::tone ( float  frequency,
uint32_t  duration,
int  channel,
bool  stop_current_sound,
const uint8_t *  raw_data,
size_t  array_len,
bool  stereo = false 
)
inline

play simple tone sound.

Parameters
frequencytone frequency (Hz)
durationtone duration (msec)
channelvirtual channel number. (0~7), (default = automatically selected)
stop_current_soundtrue=start a new output without waiting for the current one to finish.
raw_dataSingle amplitude audio data. 8bit unsigned wav.
array_lensize of raw_data.
stereotrue=data is stereo / false=data is mono.

Definition at line 147 of file Speaker_Class.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tone() [2/2]

bool m5::Speaker_Class::tone ( float  frequency,
uint32_t  duration = UINT32_MAX,
int  channel = -1,
bool  stop_current_sound = true 
)
inline

play simple tone sound.

Parameters
frequencytone frequency (Hz)
durationtone duration (msec)
channelvirtual channel number. (0~7), (default = automatically selected)

Definition at line 156 of file Speaker_Class.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ _cb_set_enabled

bool(* m5::Speaker_Class::_cb_set_enabled) (void *args, bool enabled) = nullptr
protected

Definition at line 278 of file Speaker_Class.hpp.

◆ _cb_set_enabled_args

void* m5::Speaker_Class::_cb_set_enabled_args = nullptr
protected

Definition at line 279 of file Speaker_Class.hpp.

◆ _cfg

speaker_config_t m5::Speaker_Class::_cfg
protected

Definition at line 275 of file Speaker_Class.hpp.

◆ _ch_info

channel_info_t m5::Speaker_Class::_ch_info[sound_channel_max]
protected

Definition at line 267 of file Speaker_Class.hpp.

◆ _default_tone_wav

const uint8_t m5::Speaker_Class::_default_tone_wav = { 177, 219, 246, 255, 246, 219, 177, 128, 79, 37, 10, 1, 10, 37, 79, 128 }
staticprotected

Definition at line 231 of file Speaker_Class.hpp.

◆ _master_volume

volatile uint8_t m5::Speaker_Class::_master_volume = 64
protected

Definition at line 276 of file Speaker_Class.hpp.

◆ _play_channel_bits

std::atomic<uint16_t> m5::Speaker_Class::_play_channel_bits = { 0 }
protected

Definition at line 282 of file Speaker_Class.hpp.

◆ _task_handle

TaskHandle_t m5::Speaker_Class::_task_handle = nullptr
protected

Definition at line 286 of file Speaker_Class.hpp.

◆ _task_running

volatile bool m5::Speaker_Class::_task_running = false
protected

Definition at line 281 of file Speaker_Class.hpp.

◆ _task_semaphore

volatile SemaphoreHandle_t m5::Speaker_Class::_task_semaphore = nullptr
protected

Definition at line 287 of file Speaker_Class.hpp.

◆ sound_channel_max

constexpr const size_t m5::Speaker_Class::sound_channel_max = 8
staticconstexprprotected

Definition at line 229 of file Speaker_Class.hpp.


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