f@5: /*
f@5:
f@5: Copyright (C) 2016 Queen Mary University of London
f@5: Author: Fiore Martin
f@5:
f@5: This file is part of Collidoscope.
f@5:
f@5: Collidoscope is free software: you can redistribute it and/or modify
f@5: it under the terms of the GNU General Public License as published by
f@5: the Free Software Foundation, either version 3 of the License, or
f@5: (at your option) any later version.
f@5:
f@5: This program is distributed in the hope that it will be useful,
f@5: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@5: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@5: GNU General Public License for more details.
f@5:
f@5: You should have received a copy of the GNU General Public License
f@5: along with this program. If not, see .
f@5:
f@5: This file incorporates work covered by the following copyright and permission notice:
f@5:
f@5: Copyright (c) 2014, The Cinder Project
f@5:
f@5: This code is intended to be used with the Cinder C++ library, http://libcinder.org
f@5:
f@5: Redistribution and use in source and binary forms, with or without modification, are permitted provided that
f@5: the following conditions are met:
f@5:
f@5: * Redistributions of source code must retain the above copyright notice, this list of conditions and
f@5: the following disclaimer.
f@5: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
f@5: the following disclaimer in the documentation and/or other materials provided with the distribution.
f@5:
f@5: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
f@5: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
f@5: PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
f@5: ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
f@5: TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
f@5: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
f@5: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
f@5: POSSIBILITY OF SUCH DAMAGE.
f@5:
f@5: */
f@5:
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@5: typedef std::shared_ptr BufferToWaveRecorderNodeRef;
f@0:
f@0: typedef ci::audio::dsp::RingBufferT RecordWaveMsgRingBuffer;
f@0:
f@2: /**
f@2: * A \a Node in the audio graph of the Cinder audio library that records input in a buffer.
f@2: *
f@16: * 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@16: * when recording, it uses the audio input samples to compute the size values of the visual chunks.
f@2: * 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: *
f@2: */
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@5: size_t getNumFrames() const { return mRecorderBuffer.getNumFrames(); }
f@0: //! Returns the length of the recording buffer in seconds.
f@5: 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@5: 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@2: //! returns a reference to the ring buffer when the size values of the chunks is stored, when a new wave is recorder
f@0: RecordWaveMsgRingBuffer& getRingBuffer() { return mRingBuffer; }
f@0:
f@2: //!returns a pointer to the buffer where the audio is recorder. This is used by the PGranular to create the granular synthesis
f@0: ci::audio::Buffer* getRecorderBuffer() { return &mRecorderBuffer; }
f@0:
f@0:
f@0: protected:
f@5: void initialize() override;
f@5: 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@5: ci::audio::BufferDynamic mRecorderBuffer;
f@5: ci::audio::BufferDynamicRef mCopiedBuffer;
f@5: 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: