You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.2KB

  1. #if!defined WAVWRITER_HPP
  2. #define WAVWRITER_HPP
  3. #include <dsp/ringbuffer.hpp>
  4. #include <dsp/frame.hpp>
  5. #include <thread>
  6. #include <atomic>
  7. #include <mutex>
  8. #include <vector>
  9. #include "write_wav.h"
  10. class WavWriter
  11. {
  12. static constexpr unsigned int const ChannelCount = 2u;
  13. public:
  14. using Frame = rack::Frame<ChannelCount>;
  15. enum class Errors : std::uint8_t
  16. {
  17. NoError,
  18. BufferOverflow,
  19. UnableToOpenFile
  20. };
  21. static std::string getErrorText(Errors const error);
  22. WavWriter();
  23. ~WavWriter();
  24. /*! Start writing thread
  25. \param outputFilePath Output file path. This file can be already existing, but in this case it
  26. must be writable.
  27. */
  28. void start(std::string const& outputFilePath);
  29. /*! Stop writing */
  30. void stop();
  31. bool isRunning()const { return m_running; }
  32. bool haveError()const { return m_error != Errors::NoError; }
  33. void clearError() { m_error = Errors::NoError; }
  34. Errors error()const { return m_error; }
  35. /*! Push data to the buffer. */
  36. void push(Frame const& frame);
  37. private:
  38. void run(std::string const& outputFilePath);
  39. void finishThread();
  40. private:
  41. std::vector<Frame> m_buffer;
  42. std::mutex m_mutexBuffer;
  43. std::thread m_thread;
  44. std::atomic<bool> m_running;
  45. std::atomic<Errors> m_error;
  46. };
  47. #endif