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 @ 0:02467299402e

History | View | Annotate | Download (3.48 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
16
class BufferToWaveRecorderNode : public ci::audio::SampleRecorderNode {
17
public:
18
19
    static const float kRampTime;
20
21
    //! Constructor. numChunks is the total number of chunks this biffer has to be borken down in.
22
    //! numSeconds lenght of the buffer in seconds
23
    BufferToWaveRecorderNode( std::size_t numChunks, double numSeconds );
24
25
    //! Starts recording. Resets the write position to zero (call disable() to pause recording).
26
    void start();
27
    //! Stops recording. Same as calling disable().
28
    void stop();
29
30
    //! \brief Sets the length of the recording buffer in frames.
31
    //!
32
    //! If the write position is non-zero, the old contents will be preserved (by copying it to the newly allocated Buffer).
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).
34
    void setNumFrames(size_t numFrames, bool shrinkToFit = false);
35
    //! Sets the length of the recording buffer in seconds. \see setNumFrames
36
    void setNumSeconds(double numSeconds, bool shrinkToFit = false);
37
38
    //! Returns the length of the recording buffer in frames.
39
    size_t                getNumFrames() const        { return mRecorderBuffer.getNumFrames(); }
40
    //! Returns the length of the recording buffer in seconds.
41
    double                getNumSeconds() const;
42
43
    //! \brief Returns a copy of the recored samples, up to the current write position.
44
    //!
45
    //! This method is non locking, and as such any resizing calls must be performed on the same thread or be otherwise synchronized.
46
    ci::audio::BufferRef        getRecordedCopy() const;
47
48
    //! \brief Writes the currently recorded samples to a file at \a filePath
49
    //!
50
    //! The encoding format is derived from \a filePath's extension and \a sampleType (default = SampleType::INT_16).
51
    //! \note throws AudioFileExc if the write request cannot be completed.
52
    void writeToFile(const ci::fs::path &filePath, ci::audio::SampleType sampleType = ci::audio::SampleType::INT_16);
53
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.
55
    uint64_t getLastOverrun();
56
57
    RecordWaveMsgRingBuffer& getRingBuffer() { return mRingBuffer; }
58
59
    ci::audio::Buffer* getRecorderBuffer() { return &mRecorderBuffer; }
60
61
62
protected:
63
    void initialize()                                override;
64
    void process(ci::audio::Buffer *buffer)        override;
65
66
    void initBuffers(size_t numFrames);
67
68
    static const float kMinAudioVal;
69
    static const float kMaxAudioVal;
70
71
    ci::audio::BufferDynamic                mRecorderBuffer;
72
    ci::audio::BufferDynamicRef                mCopiedBuffer;
73
    std::atomic<uint64_t>        mLastOverrun;
74
75
    RecordWaveMsgRingBuffer mRingBuffer;
76
77
    const std::size_t mNumChunks;
78
    const double mNumSeconds;
79
    std::size_t mNumSamplesPerChunk;
80
    std::atomic<std::size_t> mChunkIndex;
81
82
    size_t mChunkSampleCounter;
83
    float mChunkMaxAudioVal;
84
    float mChunkMinAudioVal;
85
86
    float mEnvRamp;
87
    float mEnvRampRate;
88
    size_t mEnvRampLen;
89
    size_t mEnvDecayStart;
90
91
};