Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Voice.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"
28 #include "cinder/audio/Source.h"
29 
30 #include <memory>
31 
32 namespace cinder { namespace audio {
33 
34 typedef std::shared_ptr<class Voice> VoiceRef;
35 typedef std::shared_ptr<class VoiceSamplePlayerNode> VoiceSamplePlayerNodeRef;
36 
47 class Voice {
48  public:
50  struct Options {
53  {}
54 
56  Options& channels( size_t ch ) { mChannels = ch; return *this; }
57 
61  Options& maxFramesForBufferPlayback( size_t frames ) { mMaxFramesForBufferPlayback = frames; return *this; }
62 
66  Options& connectToMaster( bool shouldConnect ) { mConnectToMaster = shouldConnect; return *this; }
67 
69  size_t getChannels() const { return mChannels; }
73  bool getConnectToMaster() const { return mConnectToMaster; }
74 
75  protected:
78  };
79 
80  virtual ~Voice();
81 
83  static VoiceSamplePlayerNodeRef create( const SourceFileRef &sourceFile, const Options &options = Options() );
85  static VoiceRef create( const CallbackProcessorFn &callbackFn, const Options &options = Options() );
86 
88  virtual void start();
90  virtual void stop();
92  virtual void pause();
94  virtual bool isPlaying() const;
96  virtual NodeRef getInputNode() const = 0;
98  virtual NodeRef getOutputNode() const;
99 
100  void setVolume( float volume );
101  void setPan( float pos );
102 
103  float getVolume() const;
104  float getPan() const;
105 
106  protected:
107  Voice() : mBusId( 0 ) {}
108 
109  private:
110  size_t mBusId;
111  friend class MixerImpl;
112 };
113 
119 class VoiceSamplePlayerNode : public Voice {
120  public:
121 
122  NodeRef getInputNode() const override { return mNode; }
125 
126  virtual void start() override;
127  virtual void stop() override;
128 
129  protected:
130  VoiceSamplePlayerNode( const SourceFileRef &sourceFile, const Options &options );
132 
133  friend class Voice;
134 };
135 
138  public:
139  NodeRef getInputNode() const override { return mNode; }
140 
141  protected:
142  VoiceCallbackProcessor( const CallbackProcessorFn &callbackFn, const Options &options );
143 
145  friend class Voice;
146 };
147 
148 } } // namespace cinder::audio
VoiceCallbackProcessor(const CallbackProcessorFn &callbackFn, const Options &options)
Definition: Voice.cpp:283
std::shared_ptr< class VoiceSamplePlayerNode > VoiceSamplePlayerNodeRef
Definition: Voice.h:35
friend class MixerImpl
Definition: Voice.h:111
Concrete Voice for sample playback.
Definition: Voice.h:119
virtual ~Voice()
Definition: Voice.cpp:199
std::shared_ptr< class SourceFile > SourceFileRef
Definition: Source.h:34
SamplePlayerNodeRef mNode
Definition: Voice.h:131
bool mConnectToMaster
Definition: Voice.h:77
float getPan() const
Definition: Voice.cpp:209
virtual NodeRef getInputNode() const =0
Returns the Node that is the source of this Voice's audio production. The Node type is determined by ...
virtual void stop() override
Stops the Voice, resetting its state to the same as when it was created.
Definition: Voice.cpp:274
size_t mChannels
Definition: Voice.h:76
CallbackProcessorNodeRef mNode
Definition: Voice.h:144
NodeRef getInputNode() const override
Returns the Node that is the source of this Voice's audio production. The Node type is determined by ...
Definition: Voice.h:122
std::shared_ptr< class CallbackProcessorNode > CallbackProcessorNodeRef
Definition: InputNode.h:34
Options & connectToMaster(bool shouldConnect)
Sets whether the Voice's output is automatically connected to master()->getOutput().
Definition: Voice.h:66
size_t mMaxFramesForBufferPlayback
Definition: Voice.h:76
Voice()
Definition: Voice.h:107
void setVolume(float volume)
Definition: Voice.cpp:214
virtual bool isPlaying() const
Returns whether the Voice is currently playing or not.
Definition: Voice.cpp:239
Options()
Definition: Voice.h:51
float getVolume() const
Definition: Voice.cpp:204
Options & channels(size_t ch)
Sets the number of channels for the Voice.
Definition: Voice.h:56
virtual void start()
Starts the Voice. Does nothing if currently playing.
Definition: Voice.cpp:224
virtual void pause()
Pauses the Voice inits current state. start() will resume from here.
Definition: Voice.cpp:229
SamplePlayerNodeRef getSamplePlayerNode() const
Returns a shared_ptr of the owned SamplePlayerNode.
Definition: Voice.h:124
Interface for performing high-level audio playback tasks.
Definition: Voice.h:47
virtual void stop()
Stops the Voice, resetting its state to the same as when it was created.
Definition: Voice.cpp:234
bool getConnectToMaster() const
Returns whether or not the Voice will be automatically connected to master()->getOutput().
Definition: Voice.h:73
std::function< void(Buffer *, size_t)> CallbackProcessorFn
Callback used to allow simple audio processing without subclassing a Node. First parameter is the Buf...
Definition: InputNode.h:80
VoiceSamplePlayerNode(const SourceFileRef &sourceFile, const Options &options)
Definition: Voice.cpp:253
virtual void start() override
Starts the Voice. Does nothing if currently playing.
Definition: Voice.cpp:266
static VoiceSamplePlayerNodeRef create(const SourceFileRef &sourceFile, const Options &options=Options())
Creates a Voice that manages sample playback of an audio file pointed at with sourceFile.
Definition: Voice.cpp:191
virtual NodeRef getOutputNode() const
Returns the Node that is used for connected the Voice to the greater audio graph (by default...
Definition: Voice.cpp:244
Concrete Voice for processing audio with a callback function.
Definition: Voice.h:137
std::shared_ptr< class SamplePlayerNode > SamplePlayerNodeRef
Definition: SamplePlayerNode.h:36
std::shared_ptr< class Node > NodeRef
Definition: Node.h:39
Options & maxFramesForBufferPlayback(size_t frames)
Sets the maximum number of frames acceptable for a VoiceSamplePlayerNode to use in-memory buffer play...
Definition: Voice.h:61
std::shared_ptr< class Voice > VoiceRef
Definition: Voice.h:34
size_t getMaxFramesForBufferPlayback() const
Returns the configured frame allowance for buffer playback.
Definition: Voice.h:71
Optional parameters passed into Voice::create() methods.
Definition: Voice.h:50
size_t getChannels() const
Returns the number of configured channels.
Definition: Voice.h:69
NodeRef getInputNode() const override
Returns the Node that is the source of this Voice's audio production. The Node type is determined by ...
Definition: Voice.h:139
void setPan(float pos)
Definition: Voice.cpp:219