f@0: #pragma once f@0: f@0: #include "cinder/Cinder.h" f@0: #include "cinder/audio/Node.h" f@0: #include "cinder/audio/SampleRecorderNode.h" f@0: #include "cinder/audio/dsp/RingBuffer.h" f@0: #include "cinder/Filesystem.h" f@0: f@0: #include "Messages.h" f@0: f@0: typedef std::shared_ptr BufferToWaveRecorderNodeRef; f@0: f@0: typedef ci::audio::dsp::RingBufferT RecordWaveMsgRingBuffer; f@0: f@0: f@0: class BufferToWaveRecorderNode : public ci::audio::SampleRecorderNode { f@0: public: f@0: f@0: static const float kRampTime; f@0: f@0: //! Constructor. numChunks is the total number of chunks this biffer has to be borken down in. f@0: //! numSeconds lenght of the buffer in seconds f@0: BufferToWaveRecorderNode( std::size_t numChunks, double numSeconds ); f@0: f@0: //! Starts recording. Resets the write position to zero (call disable() to pause recording). f@0: void start(); f@0: //! Stops recording. Same as calling disable(). f@0: void stop(); f@0: f@0: //! \brief Sets the length of the recording buffer in frames. f@0: //! f@0: //! If the write position is non-zero, the old contents will be preserved (by copying it to the newly allocated Buffer). f@0: //! If \a shrinkToFit is set to `true`, the internal Buffer will be down-sized if necessary, otherwise it will only re-allocate when growing while changing its dimensions to match \a numFrames (default shrinkToFit = false). f@0: void setNumFrames(size_t numFrames, bool shrinkToFit = false); f@0: //! Sets the length of the recording buffer in seconds. \see setNumFrames f@0: void setNumSeconds(double numSeconds, bool shrinkToFit = false); f@0: f@0: //! Returns the length of the recording buffer in frames. f@0: size_t getNumFrames() const { return mRecorderBuffer.getNumFrames(); } f@0: //! Returns the length of the recording buffer in seconds. f@0: double getNumSeconds() const; f@0: f@0: //! \brief Returns a copy of the recored samples, up to the current write position. f@0: //! f@0: //! This method is non locking, and as such any resizing calls must be performed on the same thread or be otherwise synchronized. f@0: ci::audio::BufferRef getRecordedCopy() const; f@0: f@0: //! \brief Writes the currently recorded samples to a file at \a filePath f@0: //! f@0: //! The encoding format is derived from \a filePath's extension and \a sampleType (default = SampleType::INT_16). f@0: //! \note throws AudioFileExc if the write request cannot be completed. f@0: void writeToFile(const ci::fs::path &filePath, ci::audio::SampleType sampleType = ci::audio::SampleType::INT_16); f@0: f@0: //! Returns the frame of the last buffer overrun or 0 if none since the last time this method was called. When this happens, it means the recorded buffer probably has skipped some frames. f@0: uint64_t getLastOverrun(); f@0: f@0: RecordWaveMsgRingBuffer& getRingBuffer() { return mRingBuffer; } f@0: f@0: ci::audio::Buffer* getRecorderBuffer() { return &mRecorderBuffer; } f@0: f@0: f@0: protected: f@0: void initialize() override; f@0: void process(ci::audio::Buffer *buffer) override; f@0: f@0: void initBuffers(size_t numFrames); f@0: f@0: static const float kMinAudioVal; f@0: static const float kMaxAudioVal; f@0: f@0: ci::audio::BufferDynamic mRecorderBuffer; f@0: ci::audio::BufferDynamicRef mCopiedBuffer; f@0: std::atomic mLastOverrun; f@0: f@0: RecordWaveMsgRingBuffer mRingBuffer; f@0: f@0: const std::size_t mNumChunks; f@0: const double mNumSeconds; f@0: std::size_t mNumSamplesPerChunk; f@0: std::atomic mChunkIndex; f@0: f@0: size_t mChunkSampleCounter; f@0: float mChunkMaxAudioVal; f@0: float mChunkMinAudioVal; f@0: f@0: float mEnvRamp; f@0: float mEnvRampRate; f@0: size_t mEnvRampLen; f@0: size_t mEnvDecayStart; f@0: f@0: }; f@0: