Skip to content

File wavplayer.h

File List > external-docs > libDaisy > src > hid > wavplayer.h

Go to the documentation of this file

Source Code

/* Current Limitations:
- 1x Playback speed only
- 16-bit, mono files only (otherwise fun weirdness can happen).
- Only 1 file playing back at a time.
- Not sure how this would interfere with trying to use the SDCard/FatFs outside of
this module. However, by using the extern'd SDFile, etc. I think that would break things.
*/
#pragma once
#ifndef DSY_WAVPLAYER_H
#define DSY_WAVPLAYER_H 
#include "daisy_core.h"
#include "util/wav_format.h"
#include "ff.h"

#define WAV_FILENAME_MAX \
    256 
namespace daisy
{
// TODO: add bitrate, samplerate, length, etc.
struct WavFileInfo
{
    WAV_FormatTypeDef raw_data;               
    char              name[WAV_FILENAME_MAX]; 
};

/* 
TODO:
- Make template-y to reduce memory usage.
*/


class WavPlayer
{
  public:
    WavPlayer() {}
    ~WavPlayer() {}

    void Init(const char* search_path);

    int Open(size_t sel);

    int Close();

    int16_t Stream();

    void Prepare();

    void Restart();

    inline void SetLooping(bool loop) { looping_ = loop; }

    inline bool GetLooping() const { return looping_; }

    inline size_t GetNumberFiles() const { return file_cnt_; }

    inline size_t GetCurrentFile() const { return file_sel_; }

  private:
    enum BufferState
    {
        BUFFER_STATE_IDLE,
        BUFFER_STATE_PREPARE_0,
        BUFFER_STATE_PREPARE_1,
    };

    BufferState GetNextBuffState();

    static constexpr size_t kMaxFiles   = 8;
    static constexpr size_t kBufferSize = 4096;
    WavFileInfo             file_info_[kMaxFiles];
    size_t                  file_cnt_, file_sel_;
    BufferState             buff_state_;
    int16_t                 buff_[kBufferSize];
    size_t                  read_ptr_;
    bool                    looping_, playing_;
    FIL                     fil_;
};

} // namespace daisy

#endif