libDaisy
Hardware Library for Daisy
Loading...
Searching...
No Matches
daisy::WavPlayer< workspace_bytes > Class Template Reference

Detailed Description

template<size_t workspace_bytes>
class daisy::WavPlayer< workspace_bytes >

WAV file Streaming Playback

At this time, this class only supports streaming of 16-bit WAV Files The output of this class will be in float converted from 16-bit integers and linearly interpolated for non-integer playback speeds.

Due to the implementation, reverse playback is not possible with this class.

The workspace_bytes template parameter is used to set the size in bytes of audio samples within the FIFO.

The bulk of amount of memory used by this class is approximately: (2 * workspace_bytes); This could hypothetically be reduced by half by directly accessing the FIFO's inner array (requires modifications to FIFO class), or making a new type of queue data structure

Whenever the Stream function results in a the samples FIFO being less than 75% full, it will generate a request for new data. So the average disk i/o transaction will be the workspace_bytes / 4. However, There are times, like when restarting playback, or opening a different file, that will trigger the entire buffer to be filled.

#include <WavPlayer.h>

Classes

struct  FileInfo
 

Public Types

enum class  Result {
  Ok , FileNotFoundError , PlaybackUnderrun , PrepareOverrun ,
  NewSamplesRequested , DiskError
}
 

Public Member Functions

 WavPlayer ()
 
 ~WavPlayer ()
 
Result Init (const char *name)
 
Result Open (const char *name)
 
Result Close ()
 
Result Prepare ()
 
Result Stream (float *samples, size_t num_channels)
 
void Restart ()
 
size_t GetDurationInSamples () const
 
size_t GetChannels () const
 
uint32_t GetPosition () const
 
float GetNormalizedPosition () const
 
void SetLooping (bool state)
 
bool GetLooping () const
 
void SetPlaying (bool state)
 
bool GetPlaying () const
 
void SetPlaybackSpeedRatio (const float speed)
 
void SetPlaybackSpeedSemitones (const float semitones)
 

Member Enumeration Documentation

◆ Result

template<size_t workspace_bytes>
enum class daisy::WavPlayer::Result
strong

Return values for status, and errors.

Enumerator
Ok 
FileNotFoundError 
PlaybackUnderrun 
PrepareOverrun 
NewSamplesRequested 
DiskError 

Constructor & Destructor Documentation

◆ WavPlayer()

template<size_t workspace_bytes>
daisy::WavPlayer< workspace_bytes >::WavPlayer ( )
inline

◆ ~WavPlayer()

template<size_t workspace_bytes>
daisy::WavPlayer< workspace_bytes >::~WavPlayer ( )
inline

Member Function Documentation

◆ Close()

template<size_t workspace_bytes>
Result daisy::WavPlayer< workspace_bytes >::Close ( )
inline

Close a file, and clear the data

◆ GetChannels()

template<size_t workspace_bytes>
size_t daisy::WavPlayer< workspace_bytes >::GetChannels ( ) const
inline

Return the number of audio channels in the open audio file

◆ GetDurationInSamples()

template<size_t workspace_bytes>
size_t daisy::WavPlayer< workspace_bytes >::GetDurationInSamples ( ) const
inline

Return the number of samples in the open audio file

◆ GetLooping()

template<size_t workspace_bytes>
bool daisy::WavPlayer< workspace_bytes >::GetLooping ( ) const
inline

Return whether the player is looping or not.

◆ GetNormalizedPosition()

template<size_t workspace_bytes>
float daisy::WavPlayer< workspace_bytes >::GetNormalizedPosition ( ) const
inline

Returns a 0-1 representation of the playhead position within the file.

◆ GetPlaying()

template<size_t workspace_bytes>
bool daisy::WavPlayer< workspace_bytes >::GetPlaying ( ) const
inline

◆ GetPosition()

template<size_t workspace_bytes>
uint32_t daisy::WavPlayer< workspace_bytes >::GetPosition ( ) const
inline

Returns the position of the playhead in samples from the start of the file

◆ Init()

template<size_t workspace_bytes>
Result daisy::WavPlayer< workspace_bytes >::Init ( const char * name)
inline

Initialize, and open a single file by name for playback

Open the file

◆ Open()

template<size_t workspace_bytes>
Result daisy::WavPlayer< workspace_bytes >::Open ( const char * name)
inline

Open a file, and prepare audio for streaming

◆ Prepare()

template<size_t workspace_bytes>
Result daisy::WavPlayer< workspace_bytes >::Prepare ( )
inline

To be executed in the main while loop, or other interruptable areas of code. This will perform the actual Disk I/O for streaming audio into the buffers used for playback.

◆ Restart()

template<size_t workspace_bytes>
void daisy::WavPlayer< workspace_bytes >::Restart ( )
inline

Clear all playback samples, and return to the beginning of the audio file immediately

◆ SetLooping()

template<size_t workspace_bytes>
void daisy::WavPlayer< workspace_bytes >::SetLooping ( bool state)
inline

Set whether the audio file will automatically continue playing from the beginning after reaching the end of file.

◆ SetPlaybackSpeedRatio()

template<size_t workspace_bytes>
void daisy::WavPlayer< workspace_bytes >::SetPlaybackSpeedRatio ( const float speed)
inline

Direct setter of playback speed as a ratio compared to original speed. For example, 1.0 equals original speed, 0.5 is half-speed, etc.

◆ SetPlaybackSpeedSemitones()

template<size_t workspace_bytes>
void daisy::WavPlayer< workspace_bytes >::SetPlaybackSpeedSemitones ( const float semitones)
inline

Sets playback speed as a number of semitones offset from original pitch For example, +7 a ratio of 1.5, +12 a ratio of 2, -12 a ratio of 0.5, etc.

◆ SetPlaying()

template<size_t workspace_bytes>
void daisy::WavPlayer< workspace_bytes >::SetPlaying ( bool state)
inline

◆ Stream()

template<size_t workspace_bytes>
Result daisy::WavPlayer< workspace_bytes >::Stream ( float * samples,
size_t num_channels )
inline

Stream Audio from disk at the current playback speed.

Each call to this will increment the playback position's internal accumulator by the playback speed. Anytime this accumulator exceeds 1.0, it will update it's position tracker, and pop the next sample from the FIFO of audio samples. Whenever the contents of the audio sample FIFO fall below 75% of it's capacity, a request is generated to refill it. The maximum playback speed possible is limited to the following factors:

  • SD Card Bus-width
  • SD Card Clock Speed
  • workspace_bytes setting (consequently, transfer sizes)

It is possible to allow higher playback speeds, and improve bandwidth by using higher workspace sizes, with the trade-offs being memory, and latency with certain transactions.

Parameters
samplesbuffer of floats to fill with audio samples from disk
num_channelsnumber of channels provided to fill. This can be different from the number of channels in the file.

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