annotate CollidoscopeApp/include/BufferToWaveRecorderNode.h @ 2:dd889fff8423

added some comments
author Fiore Martin <f.martin@qmul.ac.uk>
date Mon, 11 Jul 2016 17:03:40 +0200
parents 02467299402e
children 75b744078d66
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@2 15 /**
f@2 16 * A \a Node in the audio graph of the Cinder audio library that records input in a buffer.
f@2 17 *
f@2 18 * This class is similar to \a cinder::audio::BufferRecorderNode (it's a derivative work of this class indeed) but it has an additional feature.
f@2 19 * When recording it uses the audio input samples to compute the size values of the visual chunks.
f@2 20 * The chunks values are stored in a ring buffer and fetched by the graphic thread to paint the wave as it gets recorded.
f@2 21 *
f@2 22 */
f@0 23 class BufferToWaveRecorderNode : public ci::audio::SampleRecorderNode {
f@0 24 public:
f@0 25
f@0 26 static const float kRampTime;
f@0 27
f@0 28 //! Constructor. numChunks is the total number of chunks this biffer has to be borken down in.
f@0 29 //! numSeconds lenght of the buffer in seconds
f@0 30 BufferToWaveRecorderNode( std::size_t numChunks, double numSeconds );
f@0 31
f@0 32 //! Starts recording. Resets the write position to zero (call disable() to pause recording).
f@0 33 void start();
f@0 34 //! Stops recording. Same as calling disable().
f@0 35 void stop();
f@0 36
f@0 37 //! \brief Sets the length of the recording buffer in frames.
f@0 38 //!
f@0 39 //! If the write position is non-zero, the old contents will be preserved (by copying it to the newly allocated Buffer).
f@0 40 //! 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 41 void setNumFrames(size_t numFrames, bool shrinkToFit = false);
f@0 42 //! Sets the length of the recording buffer in seconds. \see setNumFrames
f@0 43 void setNumSeconds(double numSeconds, bool shrinkToFit = false);
f@0 44
f@0 45 //! Returns the length of the recording buffer in frames.
f@0 46 size_t getNumFrames() const { return mRecorderBuffer.getNumFrames(); }
f@0 47 //! Returns the length of the recording buffer in seconds.
f@0 48 double getNumSeconds() const;
f@0 49
f@0 50 //! \brief Returns a copy of the recored samples, up to the current write position.
f@0 51 //!
f@0 52 //! This method is non locking, and as such any resizing calls must be performed on the same thread or be otherwise synchronized.
f@0 53 ci::audio::BufferRef getRecordedCopy() const;
f@0 54
f@0 55 //! \brief Writes the currently recorded samples to a file at \a filePath
f@0 56 //!
f@0 57 //! The encoding format is derived from \a filePath's extension and \a sampleType (default = SampleType::INT_16).
f@0 58 //! \note throws AudioFileExc if the write request cannot be completed.
f@0 59 void writeToFile(const ci::fs::path &filePath, ci::audio::SampleType sampleType = ci::audio::SampleType::INT_16);
f@0 60
f@0 61 //! 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 62 uint64_t getLastOverrun();
f@0 63
f@2 64 //! returns a reference to the ring buffer when the size values of the chunks is stored, when a new wave is recorder
f@0 65 RecordWaveMsgRingBuffer& getRingBuffer() { return mRingBuffer; }
f@0 66
f@2 67 //!returns a pointer to the buffer where the audio is recorder. This is used by the PGranular to create the granular synthesis
f@0 68 ci::audio::Buffer* getRecorderBuffer() { return &mRecorderBuffer; }
f@0 69
f@0 70
f@0 71 protected:
f@0 72 void initialize() override;
f@0 73 void process(ci::audio::Buffer *buffer) override;
f@0 74
f@0 75 void initBuffers(size_t numFrames);
f@0 76
f@0 77 static const float kMinAudioVal;
f@0 78 static const float kMaxAudioVal;
f@0 79
f@0 80 ci::audio::BufferDynamic mRecorderBuffer;
f@0 81 ci::audio::BufferDynamicRef mCopiedBuffer;
f@0 82 std::atomic<uint64_t> mLastOverrun;
f@0 83
f@0 84 RecordWaveMsgRingBuffer mRingBuffer;
f@0 85
f@0 86 const std::size_t mNumChunks;
f@0 87 const double mNumSeconds;
f@0 88 std::size_t mNumSamplesPerChunk;
f@0 89 std::atomic<std::size_t> mChunkIndex;
f@0 90
f@0 91 size_t mChunkSampleCounter;
f@0 92 float mChunkMaxAudioVal;
f@0 93 float mChunkMinAudioVal;
f@0 94
f@0 95 float mEnvRamp;
f@0 96 float mEnvRampRate;
f@0 97 size_t mEnvRampLen;
f@0 98 size_t mEnvDecayStart;
f@0 99
f@0 100 };
f@0 101