Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SampleRecorderNode.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2014, The Cinder Project
3 
4  This code is intended to be used with the Cinder C++ library, http://libcinder.org
5 
6  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
7  the following conditions are met:
8 
9  * Redistributions of source code must retain the above copyright notice, this list of conditions and
10  the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
12  the following disclaimer in the documentation and/or other materials provided with the distribution.
13 
14  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
15  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
16  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
17  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
18  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
19  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21  POSSIBILITY OF SUCH DAMAGE.
22  */
23 
24 #pragma once
25 
26 #include "cinder/audio/Node.h"
28 #include "cinder/Filesystem.h"
29 
30 namespace cinder { namespace audio {
31 
32 typedef std::shared_ptr<class SampleRecorderNode> SampleRecorderNodeRef;
33 typedef std::shared_ptr<class BufferRecorderNode> BufferRecorderNodeRef;
34 
37  public:
39  size_t getWritePosition() const { return mWritePos; }
40  protected:
41  SampleRecorderNode( const Format &format = Format() );
42 
43  std::atomic<size_t> mWritePos;
44 };
45 
48  public:
49  BufferRecorderNode( const Format &format = Format() );
51  BufferRecorderNode( size_t numFrames, const Format &format = Format() );
52 
54  void start();
56  void stop();
57 
62  void setNumFrames( size_t numFrames, bool shrinkToFit = false );
64  void setNumSeconds( double numSeconds, bool shrinkToFit = false );
65 
67  size_t getNumFrames() const { return mRecorderBuffer.getNumFrames(); }
69  double getNumSeconds() const;
70 
74  BufferRef getRecordedCopy() const;
75 
80  void writeToFile( const ci::fs::path &filePath, SampleType sampleType = SampleType::INT_16 );
81 
83  uint64_t getLastOverrun();
84 
85  protected:
86  virtual void initialize() override;
87  virtual void process( Buffer *buffer ) override;
88 
89  void initBuffers( size_t numFrames );
90 
93  std::atomic<uint64_t> mLastOverrun;
94 };
95 
96 } } // namespace cinder::audio
std::atomic< size_t > mWritePos
Definition: SampleRecorderNode.h:43
std::shared_ptr< Buffer > BufferRef
Definition: Buffer.h:293
virtual void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: SampleRecorderNode.cpp:182
void setNumFrames(size_t numFrames, bool shrinkToFit=false)
Sets the length of the recording buffer in frames.
Definition: SampleRecorderNode.cpp:140
virtual void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: SampleRecorderNode.cpp:102
size_t getWritePosition() const
Returns the current write position, which represents how many samples are currently recorded...
Definition: SampleRecorderNode.h:39
BufferRef getRecordedCopy() const
Returns a copy of the recored samples, up to the current write position.
Definition: SampleRecorderNode.cpp:156
Base Node class for recording audio samples. Inherits from NodeAudioPullable, and therefore does not ...
Definition: SampleRecorderNode.h:36
BufferDynamicRef mCopiedBuffer
Definition: SampleRecorderNode.h:92
BufferDynamic mRecorderBuffer
Definition: SampleRecorderNode.h:91
std::shared_ptr< BufferDynamic > BufferDynamicRef
Definition: Buffer.h:296
uint64_t getLastOverrun()
Returns the frame of the last buffer overrun or 0 if none since the last time this method was called...
Definition: SampleRecorderNode.cpp:175
std::shared_ptr< class SampleRecorderNode > SampleRecorderNodeRef
Definition: SampleRecorderNode.h:32
BufferRecorderNode(const Format &format=Format())
Definition: SampleRecorderNode.cpp:90
void initBuffers(size_t numFrames)
Definition: SampleRecorderNode.cpp:113
size_t getNumFrames() const
Returns the number of frames in the buffer.
Definition: Buffer.h:48
GLuint buffer
Definition: GLee.h:2065
void start()
Starts recording. Resets the write position to zero (call disable() to pause recording).
Definition: SampleRecorderNode.cpp:119
SampleRecorderNode(const Format &format=Format())
Definition: SampleRecorderNode.cpp:81
Definition: Node.h:72
std::atomic< uint64_t > mLastOverrun
Definition: SampleRecorderNode.h:93
a Node that can be pulled without being connected to any outputs.
Definition: Node.h:242
void writeToFile(const ci::fs::path &filePath, SampleType sampleType=SampleType::INT_16)
Writes the currently recorded samples to a file at filePath.
Definition: SampleRecorderNode.cpp:166
void setNumSeconds(double numSeconds, bool shrinkToFit=false)
Sets the length of the recording buffer in seconds.
Definition: SampleRecorderNode.cpp:130
Records its inputs to a Buffer. The Buffer record size should be specified by the user (the default s...
Definition: SampleRecorderNode.h:47
GLenum GLsizei GLenum format
Definition: GLee.h:969
SampleType
Identifiers sample types. Primarily used for encoding audio at different bitrates.
Definition: SampleType.h:29
std::shared_ptr< class BufferRecorderNode > BufferRecorderNodeRef
Definition: SampleRecorderNode.h:33
size_t getNumFrames() const
Returns the length of the recording buffer in frames.
Definition: SampleRecorderNode.h:67
void stop()
Stops recording. Same as calling disable().
Definition: SampleRecorderNode.cpp:125
double getNumSeconds() const
Returns the length of the recording buffer in seconds.
Definition: SampleRecorderNode.cpp:135