annotate CollidoscopeApp/include/BufferToWaveRecorderNode.h @ 1:b5bcad8e7803

renamed teensy files added DeviceManageJack.cpp
author Fiore Martin <f.martin@qmul.ac.uk>
date Fri, 08 Jul 2016 11:39:18 +0200
parents 02467299402e
children dd889fff8423
rev   line source
f@0 1 #pragma once
f@0 2
f@0 3 #include "cinder/Cinder.h"
f@0 4 #include "cinder/audio/Node.h"
f@0 5 #include "cinder/audio/SampleRecorderNode.h"
f@0 6 #include "cinder/audio/dsp/RingBuffer.h"
f@0 7 #include "cinder/Filesystem.h"
f@0 8
f@0 9 #include "Messages.h"
f@0 10
f@0 11 typedef std::shared_ptr<class BufferToWaveRecorderNode> BufferToWaveRecorderNodeRef;
f@0 12
f@0 13 typedef ci::audio::dsp::RingBufferT<RecordWaveMsg> RecordWaveMsgRingBuffer;
f@0 14
f@0 15
f@0 16 class BufferToWaveRecorderNode : public ci::audio::SampleRecorderNode {
f@0 17 public:
f@0 18
f@0 19 static const float kRampTime;
f@0 20
f@0 21 //! Constructor. numChunks is the total number of chunks this biffer has to be borken down in.
f@0 22 //! numSeconds lenght of the buffer in seconds
f@0 23 BufferToWaveRecorderNode( std::size_t numChunks, double numSeconds );
f@0 24
f@0 25 //! Starts recording. Resets the write position to zero (call disable() to pause recording).
f@0 26 void start();
f@0 27 //! Stops recording. Same as calling disable().
f@0 28 void stop();
f@0 29
f@0 30 //! \brief Sets the length of the recording buffer in frames.
f@0 31 //!
f@0 32 //! If the write position is non-zero, the old contents will be preserved (by copying it to the newly allocated Buffer).
f@0 33 //! 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 34 void setNumFrames(size_t numFrames, bool shrinkToFit = false);
f@0 35 //! Sets the length of the recording buffer in seconds. \see setNumFrames
f@0 36 void setNumSeconds(double numSeconds, bool shrinkToFit = false);
f@0 37
f@0 38 //! Returns the length of the recording buffer in frames.
f@0 39 size_t getNumFrames() const { return mRecorderBuffer.getNumFrames(); }
f@0 40 //! Returns the length of the recording buffer in seconds.
f@0 41 double getNumSeconds() const;
f@0 42
f@0 43 //! \brief Returns a copy of the recored samples, up to the current write position.
f@0 44 //!
f@0 45 //! This method is non locking, and as such any resizing calls must be performed on the same thread or be otherwise synchronized.
f@0 46 ci::audio::BufferRef getRecordedCopy() const;
f@0 47
f@0 48 //! \brief Writes the currently recorded samples to a file at \a filePath
f@0 49 //!
f@0 50 //! The encoding format is derived from \a filePath's extension and \a sampleType (default = SampleType::INT_16).
f@0 51 //! \note throws AudioFileExc if the write request cannot be completed.
f@0 52 void writeToFile(const ci::fs::path &filePath, ci::audio::SampleType sampleType = ci::audio::SampleType::INT_16);
f@0 53
f@0 54 //! 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 55 uint64_t getLastOverrun();
f@0 56
f@0 57 RecordWaveMsgRingBuffer& getRingBuffer() { return mRingBuffer; }
f@0 58
f@0 59 ci::audio::Buffer* getRecorderBuffer() { return &mRecorderBuffer; }
f@0 60
f@0 61
f@0 62 protected:
f@0 63 void initialize() override;
f@0 64 void process(ci::audio::Buffer *buffer) override;
f@0 65
f@0 66 void initBuffers(size_t numFrames);
f@0 67
f@0 68 static const float kMinAudioVal;
f@0 69 static const float kMaxAudioVal;
f@0 70
f@0 71 ci::audio::BufferDynamic mRecorderBuffer;
f@0 72 ci::audio::BufferDynamicRef mCopiedBuffer;
f@0 73 std::atomic<uint64_t> mLastOverrun;
f@0 74
f@0 75 RecordWaveMsgRingBuffer mRingBuffer;
f@0 76
f@0 77 const std::size_t mNumChunks;
f@0 78 const double mNumSeconds;
f@0 79 std::size_t mNumSamplesPerChunk;
f@0 80 std::atomic<std::size_t> mChunkIndex;
f@0 81
f@0 82 size_t mChunkSampleCounter;
f@0 83 float mChunkMaxAudioVal;
f@0 84 float mChunkMinAudioVal;
f@0 85
f@0 86 float mEnvRamp;
f@0 87 float mEnvRampRate;
f@0 88 size_t mEnvRampLen;
f@0 89 size_t mEnvDecayStart;
f@0 90
f@0 91 };
f@0 92