Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Source.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/Buffer.h"
27 #include "cinder/DataSource.h"
28 
29 #include <boost/noncopyable.hpp>
30 
31 namespace cinder { namespace audio {
32 
33 typedef std::shared_ptr<class Source> SourceRef;
34 typedef std::shared_ptr<class SourceFile> SourceFileRef;
35 
36 namespace dsp {
37  class Converter;
38 }
39 
41 class Source : public boost::noncopyable {
42  public:
43  virtual ~Source();
44 
46  size_t getSampleRate() const { return mSampleRate; }
47 
49  virtual size_t getNumChannels() const = 0;
51  virtual size_t getSampleRateNative() const = 0;
52 
54  size_t getMaxFramesPerRead() const { return mMaxFramesPerRead; }
56  virtual void setMaxFramesPerRead( size_t count ) { mMaxFramesPerRead = count; }
57 
59  virtual size_t read( Buffer *buffer ) = 0;
60 
62  virtual std::string getMetaData() const { return std::string(); }
63 
64  protected:
65  Source( size_t sampleRate );
66 
69  virtual size_t performRead( Buffer *buffer, size_t bufferFrameOffset, size_t numFramesNeeded ) = 0;
71  virtual bool supportsConversion() { return false; }
73  void setSampleRate( size_t sampleRate ) { mSampleRate = sampleRate; }
74 
75  std::unique_ptr<dsp::Converter> mConverter;
77 
78  private:
79  size_t mSampleRate, mMaxFramesPerRead;
80 };
81 
83 class SourceFile : public Source {
84  public:
86  static std::unique_ptr<SourceFile> create( const DataSourceRef &dataSource, size_t sampleRate = 0 );
87  virtual ~SourceFile() {}
88 
89  size_t read( Buffer *buffer ) override;
90 
94  virtual SourceFileRef cloneWithSampleRate( size_t sampleRate ) const = 0;
95 
99  void seek( size_t readPositionFrames );
101  void seekToTime( double readPositionSeconds ) { return seek( size_t( readPositionSeconds * (double)getSampleRate() ) ); }
103  size_t getReadPosition() const { return mReadPos; }
105  double getReadPositionSeconds() const { return mReadPos / (double)getSampleRate(); }
107  size_t getNumFrames() const { return mNumFrames; }
109  double getNumSeconds() const { return (double)getNumFrames() / (double)getSampleRate(); }
110 
112  static std::vector<std::string> getSupportedExtensions();
113 
114  protected:
115  SourceFile( size_t sampleRate );
116 
118  virtual void performSeek( size_t readPositionFrames ) = 0;
120  virtual void setupSampleRateConversion();
121 
123 };
124 
126 inline SourceFileRef load( const DataSourceRef &dataSource, size_t sampleRate = 0 ) { return SourceFile::create( dataSource, sampleRate ); }
127 
128 } } // namespace cinder::audio
virtual size_t getNumChannels() const =0
Returns the number of channels.
void setSampleRate(size_t sampleRate)
Allows implementations to set the output samplerate.
Definition: Source.h:73
std::shared_ptr< Buffer > BufferRef
Definition: Buffer.h:293
virtual SourceFileRef cloneWithSampleRate(size_t sampleRate) const =0
Returns an copy of this Source with all properties identical except the sampleRate. This is useful if the SourceFile must match a samplerate that was unknown when it was first constructed.
BufferRef loadBuffer()
Loads and returns the entire contents of this SourceFile.
Definition: Source.cpp:143
GLsizei const GLchar ** string
Definition: GLee.h:2427
Loads and reads from an audio file source.
Definition: Source.h:83
std::shared_ptr< class SourceFile > SourceFileRef
Definition: Source.h:34
size_t getMaxFramesPerRead() const
Returns the maximum number of frames that can be read with one call to read().
Definition: Source.h:54
size_t getSampleRate() const
Returns the user facing samplerate (output).
Definition: Source.h:46
virtual std::string getMetaData() const
Returns the metadata, if any, as a string.
Definition: Source.h:62
virtual size_t read(Buffer *buffer)=0
Loads either as many frames as buffer can hold, or as many as there are left.
virtual void performSeek(size_t readPositionFrames)=0
Implement to perform seek. readPositionFrames is in native file units.
SourceFileRef clone() const
Returns a copy of this Source, with identical properties and pointing at the same data source...
Definition: Source.h:92
double getNumSeconds() const
Returns the length in seconds.
Definition: Source.h:109
size_t mNumFrames
Definition: Source.h:122
virtual bool supportsConversion()
Implementations should override and return true if they can provide samplerate conversion. If false (default), a Converter will be used if needed.
Definition: Source.h:71
virtual size_t performRead(Buffer *buffer, size_t bufferFrameOffset, size_t numFramesNeeded)=0
SourceFileRef load(const DataSourceRef &dataSource, size_t sampleRate=0)
Convenience method for loading a SourceFile from dataSource.
Definition: Source.h:126
static std::unique_ptr< SourceFile > create(const DataSourceRef &dataSource, size_t sampleRate=0)
Creates a new SourceFile from dataSource, with optional output samplerate. If sampleRate equals 0 the...
Definition: Source.cpp:44
GLuint buffer
Definition: GLee.h:2065
size_t read(Buffer *buffer) override
Loads either as many frames as buffer can hold, or as many as there are left.
Definition: Source.cpp:118
size_t getNumFrames() const
Returns the length in frames.
Definition: Source.h:107
GLuint GLuint GLsizei count
Definition: GLee.h:963
BufferDynamic mConverterReadBuffer
Definition: Source.h:76
virtual void setMaxFramesPerRead(size_t count)
Sets the maximum number of frames that can be read in one chunk.
Definition: Source.h:56
void seekToTime(double readPositionSeconds)
Seek to read position readPositionSeconds.
Definition: Source.h:101
virtual ~Source()
Definition: Source.cpp:86
virtual ~SourceFile()
Definition: Source.h:87
Source(size_t sampleRate)
Definition: Source.cpp:81
size_t mFileNumFrames
Definition: Source.h:122
std::shared_ptr< class Source > SourceRef
Definition: Source.h:33
double getReadPositionSeconds() const
Returns the current read position in seconds.
Definition: Source.h:105
size_t getReadPosition() const
Returns the current read position in frames.
Definition: Source.h:103
SourceFile(size_t sampleRate)
Definition: Source.cpp:90
virtual size_t getSampleRateNative() const =0
Returns the true samplerate of the Source.
std::shared_ptr< class DataSource > DataSourceRef
Definition: DataSource.h:35
std::unique_ptr< dsp::Converter > mConverter
Definition: Source.h:75
void seek(size_t readPositionFrames)
Seek the read position to readPositionFrames.
Definition: Source.cpp:180
Base class that is used to load and read from an audio source.
Definition: Source.h:41
size_t mReadPos
Definition: Source.h:122
static std::vector< std::string > getSupportedExtensions()
Returns a vector of extensions that SourceFile support for loading. Suitable for the extensions param...
Definition: Source.cpp:65
virtual void setupSampleRateConversion()
Sets up samplerate conversion if needed. Can be overridden by implementation if they handle samplerat...
Definition: Source.cpp:95