audiofile.h
Go to the documentation of this file.
#ifndef AUDIOFILE_H
#define AUDIOFILE_H
#include <string>
#include <algorithm>
#include <itpp/base/vec.h>
#include <itpp/base/mat.h>
#include <itpp/base/binfile.h>
#include <itpp/srccode/audiosample.h>
#include <itpp/itexports.h>
namespace itpp
{
class Audio_Stream_Description
{
public:
Audio_Stream_Description():_encoding(enc_unknown), _sampling_rate(0), _num_channels(0){}
Audio_Stream_Description(Audio_Encoding e, int sr, int nc = 1):
_encoding(e), _sampling_rate(sr), _num_channels(nc){}
Audio_Stream_Description& set_encoding(Audio_Encoding e) {_encoding = e; return *this;}
Audio_Stream_Description& set_sampling_rate(int sr) {_sampling_rate = sr; return *this;}
Audio_Stream_Description& set_num_channels(int nc) {_num_channels = nc; return *this;}
Audio_Stream_Description& set_description(const std::string& d) {_description = d; return *this;}
Audio_Encoding get_encoding() const {return _encoding;}
int get_sampling_rate() const {return _sampling_rate;}
int get_num_channels() const {return _num_channels;}
const std::string& get_description() const {return _description;}
private:
Audio_Encoding _encoding;
int _sampling_rate;
int _num_channels;
std::string _description;
};
inline bool is_valid(const Audio_Stream_Description& d)
{
if(!encoded_sample_size(d.get_encoding())) return false;
if(!d.get_num_channels()) return false;
return true;
}
namespace audiofile_details{
//abstract interfaces to read and write audio samples to streams
class Audio_Samples_Reader_If
{
public:
virtual bool read_sample(double& s, int ch) = 0;
virtual vec read_channel(int n, int ch) = 0;
virtual mat read(int n) = 0;
virtual std::streamoff tell() const = 0;
virtual bool seek(std::streamoff n) = 0;
virtual std::streamoff num_samples() = 0;
virtual ~Audio_Samples_Reader_If() {}
};
class Audio_Samples_Writer_If
{
public:
virtual bool write_sample(const double& s, int ch) = 0;
virtual bool write_channel(const vec& s, int ch) = 0;
//Write n samples to audio channel ch
virtual bool write(const mat& s) = 0;
virtual std::streamoff tell() const = 0;
virtual bool seek(std::streamoff n) = 0;
virtual std::streamoff num_samples() = 0;
virtual ~Audio_Samples_Writer_If() {}
};
}
class ITPP_EXPORT SND_In_File
{
public:
SND_In_File();
SND_In_File(const char* fname);
~SND_In_File();
bool open(const char* fname);
void close();
Audio_Stream_Description get_description() const {return *_description;}
bool seek_read(std::streamoff pos)
{
if((pos > _num_samples) || (pos < 0))
return false;
if(_samples_reader)
return _samples_reader->seek(pos);
else
return false;
}
std::streamoff tell_read()
{
if(_samples_reader)
return _samples_reader->tell();
else
return -1;
}
std::streamoff num_samples() const {return _num_samples;}
bool read_sample(double& s, int ch = 0)
{
if(_samples_reader)
return _samples_reader->read_sample(s,ch);
else
return false;
}
vec read_channel(int n, int ch = 0)
{
if(_samples_reader)
return _samples_reader->read_channel(n,ch);
else
return vec();
}
mat read(int n)
{
if(_samples_reader)
return _samples_reader->read(n);
else
return mat();
}
private:
bifstream _str;
std::streamoff _num_samples;
audiofile_details::Audio_Samples_Reader_If* _samples_reader;
Audio_Stream_Description* _description;
};
class ITPP_EXPORT SND_Out_File
{
public:
SND_Out_File();
SND_Out_File(const char *fname, const Audio_Stream_Description& d);
~SND_Out_File();
bool open(const char *fname, const Audio_Stream_Description& d);
void close();
Audio_Stream_Description get_description() const {return *_description;}
bool seek_write(std::streamoff pos)
{
if((pos > _num_samples) || (pos < 0))
return false;
if(_samples_writer)
return _samples_writer->seek(pos);
else
return false;
}
std::streamoff tell_write()
{
if(_samples_writer)
return _samples_writer->tell();
else
return -1;
}
std::streamoff num_samples() const {return _num_samples;}
bool write_sample(const double &s, int ch = 0)
{
if(_samples_writer){
bool ret = _samples_writer->write_sample(s,ch);
if(ret){
_num_samples = std::max(_num_samples, _samples_writer->tell());
}
return ret;
}
else
return false;
}
bool write_channel(const vec &v, int ch = 0)
{
if(_samples_writer){
bool ret = _samples_writer->write_channel(v,ch);
if(ret){
_num_samples = std::max(_num_samples, _samples_writer->tell());
}
return ret;
}
else
return false;
}
bool write(const mat &m)
{
if(_samples_writer){
bool ret = _samples_writer->write(m);
if(ret){
_num_samples = std::max(_num_samples, _samples_writer->tell());
}
return ret;
}
else
return false;
}
private:
bofstream _str;
std::streamoff _num_samples;
audiofile_details::Audio_Samples_Writer_If* _samples_writer;
Audio_Stream_Description* _description;
};
class ITPP_EXPORT SND_IO_File
{
public:
SND_IO_File();
SND_IO_File(const char *fname);
SND_IO_File(const char *fname, const Audio_Stream_Description& d);
~SND_IO_File();
bool open(const char *fname);
bool open(const char *fname, const Audio_Stream_Description& d);
void close();
Audio_Stream_Description get_description() const {return *_description;}
bool seek_read(std::streamoff pos)
{
if((pos > _num_samples) || (pos < 0))
return false;
if(_samples_reader)
return _samples_reader->seek(pos);
else
return false;
}
std::streamoff tell_read()
{
if(_samples_reader)
return _samples_reader->tell();
else
return -1;
}
bool seek_write(std::streamoff pos)
{
if((pos > _num_samples) || (pos < 0))
return false;
if(_samples_writer)
return _samples_writer->seek(pos);
else
return false;
}
std::streamoff tell_write()
{
if(_samples_writer)
return _samples_writer->tell();
else
return -1;
}
std::streamoff num_samples() const {return _num_samples;}
bool read_sample(double& s, int ch = 0)
{
if(_samples_reader)
return _samples_reader->read_sample(s,ch);
else
return false;
}
vec read_channel(int n, int ch = 0)
{
if(_samples_reader)
return _samples_reader->read_channel(n,ch);
else
return vec();
}
mat read(int n)
{
if(_samples_reader)
return _samples_reader->read(n);
else
return mat();
}
bool write_sample(const double &s, int ch = 0)
{
if(_samples_writer){
bool ret = _samples_writer->write_sample(s,ch);
if(ret){
_num_samples = std::max(_num_samples, _samples_writer->tell());
}
return ret;
}
else
return false;
}
bool write_channel(const vec &v, int ch = 0)
{
if(_samples_writer){
bool ret = _samples_writer->write_channel(v,ch);
if(ret){
_num_samples = std::max(_num_samples, _samples_writer->tell());
}
return ret;
}
else
return false;
}
bool write(const mat &m)
{
if(_samples_writer){
bool ret = _samples_writer->write(m);
if(ret){
_num_samples = std::max(_num_samples, _samples_writer->tell());
}
return ret;
}
else
return false;
}
private:
bfstream _str;
std::streamoff _num_samples;
audiofile_details::Audio_Samples_Reader_If* _samples_reader;
audiofile_details::Audio_Samples_Writer_If* _samples_writer;
Audio_Stream_Description* _description;
};
/*
\brief SAP audio file input class
\ingroup audio
ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
*/
/*
class SAP_In_File : virtual public Audio_File {
public:
// Constructor
SAP_In_File();
// Open the file {\em fname}.
SAP_In_File(const char *fname);
// Destructor
virtual ~SAP_In_File() { close(); }
// Open the file {\em fname}.
virtual bool open(const char *fname);
// Close the file.
virtual void close();
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
virtual bool seek_read(int pos);
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
virtual int tell_read();
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
bool read(vec &v);
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
bool read(vec &v, int n);
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
const char *get_header() { return header; }
protected:
char header[SAP_HEADER_SIZE];
};
*/
/*
\brief SAP audio file output class
\ingroup audio
ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
*/
/*
class SAP_Out_File : virtual public Audio_File {
public:
// Constructor
SAP_Out_File();
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
SAP_Out_File(const char *fname, const char *hdr);
// Destructor
virtual ~SAP_Out_File() { close(); }
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
bool open(const char *fname, const char *hdr);
// Old def. Removed since Sun CC gave warning.
//virtual bool open(const char *fname, const char *hdr);
// Close the file
virtual void close();
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
bool seek_write(int pos);
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
int tell_write();
// ADD DOCUMENTATION FOR THIS MEMBER!!!!!!!!!!!
virtual bool write(const vec &v);
};
*/
/*
\brief SAP audio file input and output class
\ingroup audio
ADD DETAILED DOCUMENTATION FOR THIS CLASS!!!!!!!!!!!
*/
/*
class SAP_IO_File : public SAP_In_File, public SAP_Out_File {
public:
// Constructor
SAP_IO_File() { }
// Open the file {\em fname}.
SAP_IO_File(const char *fname) { open(fname); }
// Destructor
virtual ~SAP_IO_File() { close(); }
// Open the file {\em fname}.
virtual bool open(const char *fname);
// Close the file
virtual void close();
};
*/
inline vec snd_read_channel(const char *fname, int ch = 0)
{
SND_In_File f(fname);
int ns = (int)std::min(f.num_samples(), (std::streamoff)std::numeric_limits<int>::max());
return f.read_channel(ns,ch);
}
inline vec snd_read_channel(const char *fname, int ch, int len, std::streamoff beg = 0)
{
vec ret; SND_In_File f(fname);
if(f.seek_read(beg)) ret = f.read_channel(len,ch);
return ret;
}
inline mat snd_read(const char *fname)
{
SND_In_File f(fname);
int ns = (int)std::min(f.num_samples(), (std::streamoff)std::numeric_limits<int>::max());
return f.read(ns);
}
inline mat snd_read(const char *fname, int len, std::streamoff beg = 0)
{
mat ret; SND_In_File f(fname);
if(f.seek_read(beg)) ret = f.read(len);
return ret;
}
inline bool snd_write_channel(const char *fname, const Audio_Stream_Description& descr, const vec& s, int ch = 0)
{
SND_Out_File f(fname,descr);
return f.write_channel(s,ch);
}
inline bool snd_write(const char *fname, const Audio_Stream_Description& descr, const mat& s)
{
SND_Out_File f(fname,descr);
return f.write(s);
}
/*
// Read SAP audio data
bool sap_read(const char *fname, vec &v);
// Read SAP audio data
bool sap_read(const char *fname, vec &v, int beg, int len);
// Write SAP audio data
bool sap_write(const char *fname, const vec &v, const char *hdr);
*/
} // namespace itpp
#endif // #ifndef AUDIOFILE_H
itpp::Audio_Stream_Description::set_sampling_rate
Audio_Stream_Description & set_sampling_rate(int sr)
Set sampling rate (samples per second)
Definition: audiofile.h:77
itpp::Audio_Stream_Description::Audio_Stream_Description
Audio_Stream_Description(Audio_Encoding e, int sr, int nc=1)
Construct with stream parameters: encoding e, sampling rate sr and number of audio channels nc...
Definition: audiofile.h:72
itpp::snd_write
bool snd_write(const char *fname, const Audio_Stream_Description &descr, const mat &s)
Write audio data.
Definition: audiofile.h:653
itpp::SND_IO_File::read
mat read(int n)
Read n samples from all channels starting at current position.
Definition: audiofile.h:450
itpp::SND_In_File
Class to read audio data from au file.
Definition: audiofile.h:176
itpp::is_valid
bool is_valid(const Audio_Stream_Description &d)
validity check for stream description d
Definition: audiofile.h:102
itpp::Audio_Stream_Description::get_sampling_rate
int get_sampling_rate() const
Get sampling rate (samples per second)
Definition: audiofile.h:85
itpp::SND_In_File::seek_read
bool seek_read(std::streamoff pos)
Go to sample number pos.
Definition: audiofile.h:192
itpp::SND_Out_File::tell_write
std::streamoff tell_write()
Get current position in samples.
Definition: audiofile.h:309
itpp::Audio_Stream_Description::get_num_channels
int get_num_channels() const
Get number of audio channels.
Definition: audiofile.h:87
itpp::snd_read
mat snd_read(const char *fname)
Read audio data.
Definition: audiofile.h:630
itpp::SND_IO_File::get_description
Audio_Stream_Description get_description() const
Get stream description.
Definition: audiofile.h:392
itpp::SND_Out_File
A class to write SND-files (the .au format)
Definition: audiofile.h:282
itpp::Audio_Stream_Description::set_description
Audio_Stream_Description & set_description(const std::string &d)
Set stream annotation.
Definition: audiofile.h:81
itpp::SND_Out_File::write
bool write(const mat &m)
Write audio channels from columns of the matrix m starting at current position.
Definition: audiofile.h:345
itpp::Audio_Stream_Description::get_encoding
Audio_Encoding get_encoding() const
Get encoding of audio samples.
Definition: audiofile.h:83
itpp::bfstream
Binary in/out-file Class.
Definition: binfile.h:603
itpp::bifstream
Binary Infile Class.
Definition: binfile.h:538
itpp::SND_IO_File::write
bool write(const mat &m)
Write audio channels from columns of the matrix m starting at current position.
Definition: audiofile.h:485
itpp::SND_IO_File::tell_write
std::streamoff tell_write()
Get current position to write in samples.
Definition: audiofile.h:424
itpp::min
T min(const Vec< T > &in)
Minimum value of vector.
Definition: min_max.h:125
itpp::SND_Out_File::num_samples
std::streamoff num_samples() const
Get number of samples in stream.
Definition: audiofile.h:317
itpp::SND_IO_File::read_channel
vec read_channel(int n, int ch=0)
Read n samples from channel ch starting at current position.
Definition: audiofile.h:442
mat.h
Matrix Class Definitions.
itpp::SND_In_File::num_samples
std::streamoff num_samples() const
Get number of samples in stream.
Definition: audiofile.h:211
itpp::max
T max(const Vec< T > &v)
Maximum value of vector.
Definition: min_max.h:45
itpp::SND_In_File::get_description
Audio_Stream_Description get_description() const
Get stream description.
Definition: audiofile.h:190
audiosample.h
Encoding and decoding of audio samples.
itpp::SND_In_File::tell_read
std::streamoff tell_read()
Get current position in samples.
Definition: audiofile.h:203
itpp::encoded_sample_size
std::size_t encoded_sample_size(Audio_Encoding e)
Size of encoded sample based on the encoding type e.
Definition: audiosample.h:302
itpp
itpp namespace
Definition: itmex.h:36
itpp::Audio_Stream_Description::set_encoding
Audio_Stream_Description & set_encoding(Audio_Encoding e)
Set encoding of audio samples.
Definition: audiofile.h:75
itpp::SND_In_File::read_sample
bool read_sample(double &s, int ch=0)
Read single sample s at current position to channel ch.
Definition: audiofile.h:213
itpp::bofstream
Binary Outfile Class.
Definition: binfile.h:474
itpp::Audio_Stream_Description::Audio_Stream_Description
Audio_Stream_Description()
Default ctor - creates uninitialized description.
Definition: audiofile.h:70
itpp::Audio_Stream_Description::get_description
const std::string & get_description() const
Get stream annotation.
Definition: audiofile.h:89
itpp::SND_IO_File::read_sample
bool read_sample(double &s, int ch=0)
Read single sample s at current position to channel ch.
Definition: audiofile.h:434
itpp::Audio_Encoding
Audio_Encoding
Supported encoding types for audio samples.
Definition: audiosample.h:58
itpp::SND_In_File::read_channel
vec read_channel(int n, int ch=0)
Read n samples from channel ch starting at current position.
Definition: audiofile.h:221
itpp::snd_read_channel
vec snd_read_channel(const char *fname, int ch=0)
Read audio channel.
Definition: audiofile.h:615
itpp::SND_Out_File::write_channel
bool write_channel(const vec &v, int ch=0)
Write the vector v to channel ch starting at current position.
Definition: audiofile.h:332
itpp::SND_IO_File::seek_read
bool seek_read(std::streamoff pos)
Set current position to read from pos (samples).
Definition: audiofile.h:394
itpp::Audio_Stream_Description
Description of audio stream.
Definition: audiofile.h:66
itpp::Audio_Stream_Description::set_num_channels
Audio_Stream_Description & set_num_channels(int nc)
Set number of audio channels.
Definition: audiofile.h:79
itpp::SND_IO_File::write_sample
bool write_sample(const double &s, int ch=0)
Write single sample s at current position to channel ch.
Definition: audiofile.h:459
itpp::SND_IO_File::num_samples
std::streamoff num_samples() const
Get number of samples in stream.
Definition: audiofile.h:432
itpp::SND_Out_File::get_description
Audio_Stream_Description get_description() const
Get stream description.
Definition: audiofile.h:296
itpp::SND_IO_File::write_channel
bool write_channel(const vec &v, int ch=0)
Write the vector v to channel ch starting at current position.
Definition: audiofile.h:472
itpp::SND_IO_File::seek_write
bool seek_write(std::streamoff pos)
Set current position to write to pos (samples).
Definition: audiofile.h:413
binfile.h
Binary file formats definitions.
itpp::SND_Out_File::write_sample
bool write_sample(const double &s, int ch=0)
Write single sample s at current position to channel ch.
Definition: audiofile.h:319
itpp::SND_In_File::read
mat read(int n)
Read n samples from all channels starting at current position into matrix.
Definition: audiofile.h:229
itpp::SND_IO_File
A class for doing both input and output of audio samples.
Definition: audiofile.h:374
itpp::snd_write_channel
bool snd_write_channel(const char *fname, const Audio_Stream_Description &descr, const vec &s, int ch=0)
Write audio channel from vector s using stream description descr.
Definition: audiofile.h:646
itpp::SND_Out_File::seek_write
bool seek_write(std::streamoff pos)
Set current position to write to pos (samples).
Definition: audiofile.h:298
itpp::SND_IO_File::tell_read
std::streamoff tell_read()
Get current position to read from in samples.
Definition: audiofile.h:405
vec.h
Templated Vector Class Definitions.

Generated on Sat Sep 30 2017 07:04:09 for IT++ by Doxygen 1.8.13