Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SamplePlayerNode.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/InputNode.h"
27 #include "cinder/audio/Source.h"
29 
30 #include <thread>
31 #include <mutex>
32 #include <condition_variable>
33 
34 namespace cinder { namespace audio {
35 
36 typedef std::shared_ptr<class SamplePlayerNode> SamplePlayerNodeRef;
37 typedef std::shared_ptr<class BufferPlayerNode> BufferPlayerNodeRef;
38 typedef std::shared_ptr<class FilePlayerNode> FilePlayerNodeRef;
39 
44 class SamplePlayerNode : public InputNode {
45  public:
46  virtual ~SamplePlayerNode() {}
47 
49  virtual void start();
51  virtual void stop();
53  virtual void seek( size_t positionFrames ) = 0;
54 
56  void seekToTime( double positionSeconds );
58  size_t getReadPosition() const { return mReadPos; }
60  double getReadPositionTime() const;
62  double getNumSeconds() const;
64  size_t getNumFrames() const { return mNumFrames; }
66  bool isEof() const { return mIsEof; }
67 
69  void setLoopEnabled( bool b = true ) { mLoop = b; }
71  bool isLoopEnabled() const { return mLoop; }
73  void setLoopBegin( size_t positionFrames );
75  void setLoopBeginTime( double positionSeconds );
77  void setLoopEnd( size_t positionFrames );
79  void setLoopEndTime( double positionSeconds );
81  size_t getLoopBegin() const { return mLoopBegin; }
83  double getLoopBeginTime() const;
85  size_t getLoopEnd() const { return mLoopEnd; }
87  double getLoopEndTime() const;
88 
89  protected:
90  SamplePlayerNode( const Format &format = Format() );
91 
92  size_t mNumFrames;
93  std::atomic<size_t> mReadPos, mLoopBegin, mLoopEnd;
94  std::atomic<bool> mLoop, mIsEof;
95 };
96 
99  public:
101  BufferPlayerNode( const Format &format = Format() );
103  BufferPlayerNode( const BufferRef &buffer, const Format &format = Format() );
104 
105  virtual ~BufferPlayerNode() {}
106 
107  virtual void seek( size_t readPositionFrames ) override;
108 
110  void loadBuffer( const SourceFileRef &sourceFile );
112  void setBuffer( const BufferRef &buffer );
114  const BufferRef& getBuffer() const { return mBuffer; }
115 
116  protected:
117  virtual void enableProcessing() override;
118  virtual void process( Buffer *buffer ) override;
119 
121 };
122 
125  public:
127  FilePlayerNode( const Format &format = Format() );
129  FilePlayerNode( const SourceFileRef &sourceFile, bool isReadAsync = true, const Format &format = Node::Format() );
130  virtual ~FilePlayerNode();
131 
132  virtual void stop() override;
133  virtual void seek( size_t readPositionFrames ) override;
134 
136  bool isReadAsync() const { return mIsReadAsync; }
137 
139  void setSourceFile( const SourceFileRef &sourceFile );
140  const SourceFileRef& getSourceFile() const { return mSourceFile; }
141 
143  uint64_t getLastUnderrun();
145  uint64_t getLastOverrun();
146 
147  protected:
148  void initialize() override;
149  void uninitialize() override;
150  void enableProcessing() override;
151  void disableProcessing() override;
152  void process( Buffer *buffer ) override;
153 
154  void readAsyncImpl();
155  void readImpl();
156  void seekImpl( size_t readPos );
157  void destroyReadThreadImpl();
158 
159  std::vector<dsp::RingBuffer> mRingBuffers; // used to transfer samples from io to audio thread, one ring buffer per channel
160  BufferDynamic mIoBuffer; // used to read samples from the file on read thread, resizeable so the ringbuffer can be filled
161 
164  std::atomic<uint64_t> mLastUnderrun, mLastOverrun;
165 
166  std::unique_ptr<std::thread> mReadThread;
167  std::mutex mAsyncReadMutex;
168  std::condition_variable mIssueAsyncReadCond;
170 };
171 
172 } } // namespace cinder::audio
std::atomic< size_t > mReadPos
Definition: SamplePlayerNode.h:93
std::shared_ptr< Buffer > BufferRef
Definition: Buffer.h:293
size_t getReadPosition() const
Returns the current read position in frames.
Definition: SamplePlayerNode.h:58
std::shared_ptr< class FilePlayerNode > FilePlayerNodeRef
Definition: SamplePlayerNode.h:38
SamplePlayerNode(const Format &format=Format())
Definition: SamplePlayerNode.cpp:38
std::shared_ptr< class SourceFile > SourceFileRef
Definition: Source.h:34
std::shared_ptr< class BufferPlayerNode > BufferPlayerNodeRef
Definition: SamplePlayerNode.h:37
void seekImpl(size_t readPos)
Definition: SamplePlayerNode.cpp:433
void setLoopEndTime(double positionSeconds)
Sets the end loop marker in seconds (default = getNumSeconds(), max = getNumSeconds()).
Definition: SamplePlayerNode.cpp:79
void seekToTime(double positionSeconds)
Seek to read position readPositionSeconds,.
Definition: SamplePlayerNode.cpp:69
bool mAsyncReadShouldQuit
Definition: SamplePlayerNode.h:169
std::atomic< size_t > mLoopBegin
Definition: SamplePlayerNode.h:93
void readAsyncImpl()
Definition: SamplePlayerNode.cpp:385
BufferDynamic mIoBuffer
Definition: SamplePlayerNode.h:160
void setLoopBegin(size_t positionFrames)
Sets the begin loop marker in frames (default = 0, max = getNumFrames()).
Definition: SamplePlayerNode.cpp:56
std::vector< dsp::RingBuffer > mRingBuffers
Definition: SamplePlayerNode.h:159
virtual void seek(size_t positionFrames)=0
Seek the read position to readPositionFrames.
double getLoopBeginTime() const
Returns the begin loop marker in seconds.
Definition: SamplePlayerNode.cpp:89
File-based SamplePlayerNode, where samples are constantly streamed from file. Suitable for large audi...
Definition: SamplePlayerNode.h:124
virtual ~FilePlayerNode()
Definition: SamplePlayerNode.cpp:226
virtual void enableProcessing() override
Callled when this Node should enable processing. Initiated from Node::enable().
Definition: SamplePlayerNode.cpp:123
uint64_t getLastOverrun()
Returns the frame of the last buffer overrun or 0 if none since the last time this method was called...
Definition: SamplePlayerNode.cpp:342
size_t mBufferFramesThreshold
Definition: SamplePlayerNode.h:163
BufferRef mBuffer
Definition: SamplePlayerNode.h:120
bool isLoopEnabled() const
Gets whether playing continues from beginning after the end is reached (default = false) ...
Definition: SamplePlayerNode.h:71
void setBuffer(const BufferRef &buffer)
Sets the current Buffer. Safe to do while enabled.
Definition: SamplePlayerNode.cpp:139
void setLoopEnabled(bool b=true)
Sets whether playing continues from beginning after the end is reached (default = false) ...
Definition: SamplePlayerNode.h:69
void readImpl()
Definition: SamplePlayerNode.cpp:404
BufferPlayerNode(const Format &format=Format())
Constructs a BufferPlayerNode without a buffer, with the assumption one will be set later...
Definition: SamplePlayerNode.cpp:108
void loadBuffer(const SourceFileRef &sourceFile)
Loads and stores a reference to a Buffer created from the entire contents of sourceFile.
Definition: SamplePlayerNode.cpp:162
std::mutex mAsyncReadMutex
Definition: SamplePlayerNode.h:167
size_t getNumFrames() const
Returns the total number of frames this SamplePlayerNode will play from beginning to end...
Definition: SamplePlayerNode.h:64
bool isReadAsync() const
Returns whether reading occurs asynchronously (default is false). If true, file reading is done from ...
Definition: SamplePlayerNode.h:136
void setLoopEnd(size_t positionFrames)
Sets the end loop marker in frames (default = getNumFrames(), max = getNumFrames()).
Definition: SamplePlayerNode.cpp:61
virtual void seek(size_t readPositionFrames) override
Seek the read position to readPositionFrames.
Definition: SamplePlayerNode.cpp:133
virtual void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: SamplePlayerNode.cpp:173
size_t getLoopEnd() const
Returns the end loop marker in frames.
Definition: SamplePlayerNode.h:85
double getNumSeconds() const
Returns the total number of seconds this SamplePlayerNode will play from beginning to end...
Definition: SamplePlayerNode.cpp:99
GLuint buffer
Definition: GLee.h:2065
uint64_t getLastUnderrun()
Returns the frame of the last buffer underrun or 0 if none since the last time this method was called...
Definition: SamplePlayerNode.cpp:335
virtual void stop()
Stops playing the sample, returns the read position to the beginning and disables processing...
Definition: SamplePlayerNode.cpp:50
double getReadPositionTime() const
Returns the current read position in seconds.
Definition: SamplePlayerNode.cpp:84
virtual void stop() override
Stops playing the sample, returns the read position to the beginning and disables processing...
Definition: SamplePlayerNode.cpp:278
virtual void seek(size_t readPositionFrames) override
Seek the read position to readPositionFrames.
Definition: SamplePlayerNode.cpp:293
void setSourceFile(const SourceFileRef &sourceFile)
Definition: SamplePlayerNode.cpp:307
Definition: Node.h:72
std::unique_ptr< std::thread > mReadThread
Definition: SamplePlayerNode.h:166
GLboolean GLboolean GLboolean b
Definition: GLee.h:2964
virtual ~BufferPlayerNode()
Definition: SamplePlayerNode.h:105
void enableProcessing() override
Callled when this Node should enable processing. Initiated from Node::enable().
Definition: SamplePlayerNode.cpp:264
size_t getLoopBegin() const
Returns the begin loop marker in frames.
Definition: SamplePlayerNode.h:81
void initialize() override
Called before audio buffers need to be used. There is always a valid Context at this point...
Definition: SamplePlayerNode.cpp:232
std::atomic< uint64_t > mLastOverrun
Definition: SamplePlayerNode.h:164
InputNode is the base class for Node's that produce audio. It cannot have any inputs.
Definition: InputNode.h:39
bool mIsReadAsync
Definition: SamplePlayerNode.h:169
const BufferRef & getBuffer() const
returns a shared_ptr to the current Buffer.
Definition: SamplePlayerNode.h:114
void disableProcessing() override
Callled when this Node should disable processing. Initiated from Node::disable(). ...
Definition: SamplePlayerNode.cpp:274
Buffer-based SamplePlayerNode, where all samples are loaded into memory before playback.
Definition: SamplePlayerNode.h:98
size_t mRingBufferPaddingFactor
Definition: SamplePlayerNode.h:163
std::atomic< size_t > mLoopEnd
Definition: SamplePlayerNode.h:93
double getLoopEndTime() const
Returns the end loop marker in seconds.
Definition: SamplePlayerNode.cpp:94
virtual void start()
Starts playing the sample from the beginning.
Definition: SamplePlayerNode.cpp:44
virtual ~SamplePlayerNode()
Definition: SamplePlayerNode.h:46
GLenum GLsizei GLenum format
Definition: GLee.h:969
void setLoopBeginTime(double positionSeconds)
Sets the begin loop marker in seconds (default = 0, max = getNumSeconds()).
Definition: SamplePlayerNode.cpp:74
SourceFileRef mSourceFile
Definition: SamplePlayerNode.h:162
std::atomic< uint64_t > mLastUnderrun
Definition: SamplePlayerNode.h:164
FilePlayerNode(const Format &format=Format())
Constructs a FilePlayerNode with optional format.
Definition: SamplePlayerNode.cpp:207
std::shared_ptr< class SamplePlayerNode > SamplePlayerNodeRef
Definition: SamplePlayerNode.h:36
size_t mNumFrames
Definition: SamplePlayerNode.h:92
std::atomic< bool > mLoop
Definition: SamplePlayerNode.h:94
std::atomic< bool > mIsEof
Definition: SamplePlayerNode.h:94
void destroyReadThreadImpl()
Definition: SamplePlayerNode.cpp:442
std::condition_variable mIssueAsyncReadCond
Definition: SamplePlayerNode.h:168
bool isEof() const
Returns whether the SamplePlayerNode has reached EOF (end of file). If true, isEnabled() will also re...
Definition: SamplePlayerNode.h:66
Base Node class for sampled audio playback. Can do operations like seek and loop. ...
Definition: SamplePlayerNode.h:44
void uninitialize() override
Called once the contents of initialize are no longer relevant, i.e. connections have changed...
Definition: SamplePlayerNode.cpp:259
const SourceFileRef & getSourceFile() const
Definition: SamplePlayerNode.h:140
void process(Buffer *buffer) override
Override to perform audio processing on buffer.
Definition: SamplePlayerNode.cpp:349