To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.19 KB)

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