Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Device.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/Function.h"
27 
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include <boost/noncopyable.hpp>
33 
34 namespace cinder { namespace audio {
35 
36 typedef std::shared_ptr<class Device> DeviceRef;
37 
39 class Device : public std::enable_shared_from_this<Device>, boost::noncopyable {
40  public:
41  virtual ~Device() {}
42 
44  static DeviceRef getDefaultOutput();
46  static DeviceRef getDefaultInput();
48  static DeviceRef findDeviceByName( const std::string &name );
50  static DeviceRef findDeviceByKey( const std::string &key );
52  static const std::vector<DeviceRef>& getDevices();
54  static std::vector<DeviceRef> getOutputDevices();
56  static std::vector<DeviceRef> getInputDevices();
57 
59  const std::string& getName();
61  const std::string& getKey();
63  size_t getNumInputChannels();
65  size_t getNumOutputChannels();
67  size_t getSampleRate();
69  size_t getFramesPerBlock();
70 
72  struct Format {
73  Format() : mSampleRate( 0 ), mFramesPerBlock( 0 ) {}
74 
76  Format& sampleRate( size_t sr ) { mSampleRate = sr; return *this; }
78  Format& framesPerBlock( size_t frames ) { mFramesPerBlock = frames; return *this; }
79 
81  size_t getSampleRate() const { return mSampleRate; }
83  size_t getFramesPerBlock() const { return mFramesPerBlock; }
84 
85  private:
86  size_t mSampleRate, mFramesPerBlock;
87  };
88 
90  void updateFormat( const Format &format );
92  signals::signal<void()>& getSignalParamsWillChange() { return mSignalParamsWillChange; }
94  signals::signal<void()>& getSignalParamsDidChange() { return mSignalParamsDidChange; }
95 
98 
99  private:
100  Device( const std::string &key ) : mKey( key ), mSampleRate( 0 ), mFramesPerBlock( 0 ) {}
101 
102  std::string mKey, mName;
103  size_t mSampleRate, mFramesPerBlock;
104  signals::signal<void()> mSignalParamsWillChange, mSignalParamsDidChange;
105 
106  friend class DeviceManager;
107 };
108 
110 class DeviceManager : public boost::noncopyable {
111  public:
112  virtual ~DeviceManager() {}
113 
114  virtual DeviceRef findDeviceByName( const std::string &name );
115  virtual DeviceRef findDeviceByKey( const std::string &key );
116 
117  virtual const std::vector<DeviceRef>& getDevices() = 0;
118  virtual DeviceRef getDefaultOutput() = 0;
119  virtual DeviceRef getDefaultInput() = 0;
120 
121  virtual std::string getName( const DeviceRef &device ) = 0;
122  virtual size_t getNumInputChannels( const DeviceRef &device ) = 0;
123  virtual size_t getNumOutputChannels( const DeviceRef &device ) = 0;
124  virtual size_t getSampleRate( const DeviceRef &device ) = 0;
125  virtual size_t getFramesPerBlock( const DeviceRef &device ) = 0;
126 
127  virtual void setSampleRate( const DeviceRef &device, size_t sampleRate ) = 0;
128  virtual void setFramesPerBlock( const DeviceRef &device, size_t framesPerBlock ) = 0;
129 
131  virtual bool isFormatUpdatedAsync() const { return false; }
132 
133  protected:
135 
136  DeviceRef addDevice( const std::string &key );
137 
138  void emitParamsWillChange( const DeviceRef &device );
139  void emitParamsDidChange( const DeviceRef &device );
140 
141  std::vector<DeviceRef> mDevices;
142 };
143 
144 } } // namespace cinder::audio
Format & framesPerBlock(size_t frames)
Sets the frames per block if the Format (only functional on Mac).
Definition: Device.h:78
virtual bool isFormatUpdatedAsync() const
override if subclass needs to update params async, and will issue formatWillChange callbacks ...
Definition: Device.h:131
static DeviceRef findDeviceByKey(const std::string &key)
Finds and returns a reference to the unique Device located by key, an platform-specific defined ident...
Definition: Device.cpp:63
Format()
Definition: Device.h:73
signals::signal< void()> & getSignalParamsDidChange()
Returns a signal that notifies connected slots after the format of this Device has changed...
Definition: Device.h:94
const std::string & getName()
Returns the name of this Device, which is a human readable string reported by the system...
Definition: Device.cpp:95
static std::vector< DeviceRef > getOutputDevices()
Returns a vector of all output Device's.
Definition: Device.cpp:73
static std::vector< DeviceRef > getInputDevices()
Returns a vector of all input Device's.
Definition: Device.cpp:84
GLsizei const GLchar ** string
Definition: GLee.h:2427
DeviceManager()
Definition: Device.h:134
const std::string & getKey()
Returns the key of this Device, which is a unique platform-specific defined identifier.
Definition: Device.cpp:103
void emitParamsDidChange(const DeviceRef &device)
Definition: Device.cpp:217
static DeviceRef findDeviceByName(const std::string &name)
Finds and returns a reference to the first Device named name.
Definition: Device.cpp:58
signals::signal< void()> & getSignalParamsWillChange()
Returns a signal that notifies connected slots before the format of this Device will change...
Definition: Device.h:92
virtual ~DeviceManager()
Definition: Device.h:112
size_t getFramesPerBlock() const
Returns the frames per block of the Format.
Definition: Device.h:83
Format & sampleRate(size_t sr)
Sets the samplerate if the Format.
Definition: Device.h:76
size_t getNumOutputChannels()
Returns the number of output channels this Device supports.
Definition: Device.cpp:113
virtual size_t getSampleRate(const DeviceRef &device)=0
virtual size_t getFramesPerBlock(const DeviceRef &device)=0
virtual void setSampleRate(const DeviceRef &device, size_t sampleRate)=0
virtual DeviceRef getDefaultInput()=0
virtual ~Device()
Definition: Device.h:41
static DeviceRef getDefaultOutput()
Returns a reference to the default output Device on your system.
Definition: Device.cpp:48
size_t getFramesPerBlock()
Returns the current frames per block.
Definition: Device.cpp:126
void updateFormat(const Format &format)
Configures the format properties of this Device. This effects the hardware on your system...
Definition: Device.cpp:134
virtual DeviceRef findDeviceByKey(const std::string &key)
Definition: Device.cpp:188
virtual size_t getNumInputChannels(const DeviceRef &device)=0
static const std::vector< DeviceRef > & getDevices()
Returns a vector of all Device's.
Definition: Device.cpp:68
Platform-specific Singleton for managing hardware devices. Applications normally should not need to u...
Definition: Device.h:110
virtual const std::vector< DeviceRef > & getDevices()=0
Object representing a hardware audio device. There is only ever one device per hardware device report...
Definition: Device.h:39
size_t getSampleRate() const
Returns the samplerate of the Format.
Definition: Device.h:81
virtual void setFramesPerBlock(const DeviceRef &device, size_t framesPerBlock)=0
virtual DeviceRef findDeviceByName(const std::string &name)
Definition: Device.cpp:178
Defines the format parameters that are settable when passed in with updateFormat() ...
Definition: Device.h:72
std::shared_ptr< class Device > DeviceRef
Definition: Device.h:36
std::vector< DeviceRef > mDevices
Definition: Device.h:141
virtual size_t getNumOutputChannels(const DeviceRef &device)=0
virtual DeviceRef getDefaultOutput()=0
GLuint const GLchar * name
Definition: GLee.h:2259
DeviceRef addDevice(const std::string &key)
Definition: Device.cpp:198
size_t getSampleRate()
Returns the current samplerate.
Definition: Device.cpp:118
static DeviceRef getDefaultInput()
Returns a reference to the default input Device on your system.
Definition: Device.cpp:53
size_t getNumInputChannels()
Returns the number of input channels this Device supports.
Definition: Device.cpp:108
GLenum GLsizei GLenum format
Definition: GLee.h:969
static std::string printDevicesToString()
Returns a string representation of all devices for debugging purposes.
Definition: Device.cpp:160
void emitParamsWillChange(const DeviceRef &device)
Definition: Device.cpp:212
virtual std::string getName(const DeviceRef &device)=0