f@5
|
1 /*
|
f@5
|
2
|
f@5
|
3 Copyright (C) 2016 Queen Mary University of London
|
f@5
|
4 Author: Fiore Martin
|
f@5
|
5
|
f@5
|
6 This file is part of Collidoscope.
|
f@5
|
7
|
f@5
|
8 Collidoscope is free software: you can redistribute it and/or modify
|
f@5
|
9 it under the terms of the GNU General Public License as published by
|
f@5
|
10 the Free Software Foundation, either version 3 of the License, or
|
f@5
|
11 (at your option) any later version.
|
f@5
|
12
|
f@5
|
13 This program is distributed in the hope that it will be useful,
|
f@5
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
f@5
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
f@5
|
16 GNU General Public License for more details.
|
f@5
|
17
|
f@5
|
18 You should have received a copy of the GNU General Public License
|
f@5
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
|
f@5
|
20
|
f@5
|
21 This file incorporates work covered by the following copyright and permission notice:
|
f@5
|
22
|
f@5
|
23 Copyright (c) 2014, The Cinder Project
|
f@5
|
24
|
f@5
|
25 This code is intended to be used with the Cinder C++ library, http://libcinder.org
|
f@5
|
26
|
f@5
|
27 Redistribution and use in source and binary forms, with or without modification, are permitted provided that
|
f@5
|
28 the following conditions are met:
|
f@5
|
29
|
f@5
|
30 * Redistributions of source code must retain the above copyright notice, this list of conditions and
|
f@5
|
31 the following disclaimer.
|
f@5
|
32 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
f@5
|
33 the following disclaimer in the documentation and/or other materials provided with the distribution.
|
f@5
|
34
|
f@5
|
35 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
|
f@5
|
36 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
f@5
|
37 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
|
f@5
|
38 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
f@5
|
39 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
f@5
|
40 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
f@5
|
41 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
f@5
|
42 POSSIBILITY OF SUCH DAMAGE.
|
f@5
|
43
|
f@5
|
44 */
|
f@5
|
45
|
f@0
|
46 #pragma once
|
f@0
|
47
|
f@0
|
48 #include "cinder/Cinder.h"
|
f@0
|
49 #include "cinder/audio/Node.h"
|
f@0
|
50 #include "cinder/audio/SampleRecorderNode.h"
|
f@0
|
51 #include "cinder/audio/dsp/RingBuffer.h"
|
f@0
|
52 #include "cinder/Filesystem.h"
|
f@0
|
53
|
f@0
|
54 #include "Messages.h"
|
f@0
|
55
|
f@5
|
56 typedef std::shared_ptr<class BufferToWaveRecorderNode> BufferToWaveRecorderNodeRef;
|
f@0
|
57
|
f@0
|
58 typedef ci::audio::dsp::RingBufferT<RecordWaveMsg> RecordWaveMsgRingBuffer;
|
f@0
|
59
|
f@2
|
60 /**
|
f@2
|
61 * A \a Node in the audio graph of the Cinder audio library that records input in a buffer.
|
f@2
|
62 *
|
f@16
|
63 * 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
|
64 * when recording, it uses the audio input samples to compute the size values of the visual chunks.
|
f@2
|
65 * 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
|
66 *
|
f@2
|
67 */
|
f@0
|
68 class BufferToWaveRecorderNode : public ci::audio::SampleRecorderNode {
|
f@0
|
69 public:
|
f@0
|
70
|
f@0
|
71 static const float kRampTime;
|
f@0
|
72
|
f@0
|
73 //! Constructor. numChunks is the total number of chunks this biffer has to be borken down in.
|
f@0
|
74 //! numSeconds lenght of the buffer in seconds
|
f@0
|
75 BufferToWaveRecorderNode( std::size_t numChunks, double numSeconds );
|
f@0
|
76
|
f@0
|
77 //! Starts recording. Resets the write position to zero (call disable() to pause recording).
|
f@0
|
78 void start();
|
f@0
|
79 //! Stops recording. Same as calling disable().
|
f@0
|
80 void stop();
|
f@0
|
81
|
f@0
|
82 //! \brief Sets the length of the recording buffer in frames.
|
f@0
|
83 //!
|
f@0
|
84 //! If the write position is non-zero, the old contents will be preserved (by copying it to the newly allocated Buffer).
|
f@0
|
85 //! 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
|
86 void setNumFrames(size_t numFrames, bool shrinkToFit = false);
|
f@0
|
87 //! Sets the length of the recording buffer in seconds. \see setNumFrames
|
f@0
|
88 void setNumSeconds(double numSeconds, bool shrinkToFit = false);
|
f@0
|
89
|
f@0
|
90 //! Returns the length of the recording buffer in frames.
|
f@5
|
91 size_t getNumFrames() const { return mRecorderBuffer.getNumFrames(); }
|
f@0
|
92 //! Returns the length of the recording buffer in seconds.
|
f@5
|
93 double getNumSeconds() const;
|
f@0
|
94
|
f@0
|
95 //! \brief Returns a copy of the recored samples, up to the current write position.
|
f@0
|
96 //!
|
f@0
|
97 //! This method is non locking, and as such any resizing calls must be performed on the same thread or be otherwise synchronized.
|
f@5
|
98 ci::audio::BufferRef getRecordedCopy() const;
|
f@0
|
99
|
f@0
|
100 //! \brief Writes the currently recorded samples to a file at \a filePath
|
f@0
|
101 //!
|
f@0
|
102 //! The encoding format is derived from \a filePath's extension and \a sampleType (default = SampleType::INT_16).
|
f@0
|
103 //! \note throws AudioFileExc if the write request cannot be completed.
|
f@0
|
104 void writeToFile(const ci::fs::path &filePath, ci::audio::SampleType sampleType = ci::audio::SampleType::INT_16);
|
f@0
|
105
|
f@0
|
106 //! 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
|
107 uint64_t getLastOverrun();
|
f@0
|
108
|
f@2
|
109 //! returns a reference to the ring buffer when the size values of the chunks is stored, when a new wave is recorder
|
f@0
|
110 RecordWaveMsgRingBuffer& getRingBuffer() { return mRingBuffer; }
|
f@0
|
111
|
f@2
|
112 //!returns a pointer to the buffer where the audio is recorder. This is used by the PGranular to create the granular synthesis
|
f@0
|
113 ci::audio::Buffer* getRecorderBuffer() { return &mRecorderBuffer; }
|
f@0
|
114
|
f@0
|
115
|
f@0
|
116 protected:
|
f@5
|
117 void initialize() override;
|
f@5
|
118 void process(ci::audio::Buffer *buffer) override;
|
f@0
|
119
|
f@0
|
120 void initBuffers(size_t numFrames);
|
f@0
|
121
|
f@0
|
122 static const float kMinAudioVal;
|
f@0
|
123 static const float kMaxAudioVal;
|
f@0
|
124
|
f@5
|
125 ci::audio::BufferDynamic mRecorderBuffer;
|
f@5
|
126 ci::audio::BufferDynamicRef mCopiedBuffer;
|
f@5
|
127 std::atomic<uint64_t> mLastOverrun;
|
f@0
|
128
|
f@0
|
129 RecordWaveMsgRingBuffer mRingBuffer;
|
f@0
|
130
|
f@0
|
131 const std::size_t mNumChunks;
|
f@0
|
132 const double mNumSeconds;
|
f@0
|
133 std::size_t mNumSamplesPerChunk;
|
f@0
|
134 std::atomic<std::size_t> mChunkIndex;
|
f@0
|
135
|
f@0
|
136 size_t mChunkSampleCounter;
|
f@0
|
137 float mChunkMaxAudioVal;
|
f@0
|
138 float mChunkMinAudioVal;
|
f@0
|
139
|
f@0
|
140 float mEnvRamp;
|
f@0
|
141 float mEnvRampRate;
|
f@0
|
142 size_t mEnvRampLen;
|
f@0
|
143 size_t mEnvDecayStart;
|
f@0
|
144
|
f@0
|
145 };
|
f@0
|
146
|