RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
audio.hpp
Go to the documentation of this file.
1 #ifndef RAPP_OBJECT_AUDIO
2 #define RAPP_OBJECT_AUDIO
3 #include "includes.ihh"
4 namespace rapp {
5 namespace object {
13 class audio
14 {
15 public:
17  audio(const std::string filepath)
18  {
19  std::ifstream bytestream(filepath,
20  std::ios::in |
21  std::ios::binary |
22  std::ios::ate);
23  if (!bytestream.is_open()) {
24  throw std::runtime_error("could not open bytestream for "+filepath);
25  }
26  else {
27  read_bytes(bytestream);
28  }
29  }
30 
33  audio(std::ifstream & bytestream)
34  {
35  read_bytes(bytestream);
36  }
37 
40  audio(std::vector<rapp::types::byte> bytearray)
41  : bytearray_(bytearray)
42  {}
43 
45  audio(const audio &) = default;
46 
48  std::vector<rapp::types::byte> bytearray() const
49  {
50  return bytearray_;
51  }
52 
54  bool operator==(const audio & rhs) const
55  {
56  return typeid(*this) == typeid(rhs) &&
57  (this->bytearray_ == rhs.bytearray_);
58  }
59 
61  audio & operator=(const audio &) = default;
62 
64  bool save(const std::string filepath)
65  {
66  std::ofstream os(filepath, std::ios::out | std::ofstream::binary);
67  if (os.is_open())
68  {
69  std::copy(bytearray_.begin(), bytearray_.end(),
70  std::ostreambuf_iterator<rapp::types::byte>(os));
71  os.close();
72  return true;
73  }
74  else
75  return false;
76  }
77 
78  virtual std::string audio_source() const
79  { return ""; }
80 
81  virtual std::string extension() const
82  { return ""; }
83 
84 private:
85 
86  // Delete empty constructor
87  audio() = delete;
88 
89  // Copy the bytestream into the bytearray
90  void read_bytes(std::ifstream & bytestream)
91  {
92  bytestream.seekg(0, std::ios_base::end);
93  std::streampos fileSize = bytestream.tellg();
94  bytearray_.resize(fileSize);
95  bytestream.seekg(0, std::ios_base::beg);
96  bytestream.read(&bytearray_[0], fileSize);
97  }
98 
99  // Actual bytes of audio file
100  std::vector<rapp::types::byte> bytearray_;
101 };
103 class ogg : public audio
104 {
105 public:
106 
107  ogg(const std::string filepath)
108  : audio(filepath){}
109 
110  ogg(std::ifstream & bytestream)
111  : audio(bytestream){}
112 
113  std::string audio_source() const
114  { return "nao_ogg";}
115 
116  std::string extension() const
117  { return ".ogg";}
118 };
121 {
122 public:
123 
124  nao_single_channel_wav(const std::string filepath)
125  : audio (filepath){}
126 
127  nao_single_channel_wav(std::ifstream & bytestream)
128  : audio (bytestream){}
129 
130  std::string audio_source() const
131  { return "nao_wav_1_ch"; }
132 
133  std::string extension() const
134  { return ".wav"; }
135 };
138 {
139 public:
140 
141  nao_quad_channel_wav(const std::string filepath)
142  : audio (filepath){}
143 
144  nao_quad_channel_wav(std::ifstream & bytestream)
145  : audio (bytestream){}
146 
147  std::string audio_source() const
148  { return "nao_wav_4_ch";}
149 
150  std::string extension() const
151  { return ".wav";}
152 };
154 class microphone_wav : public audio
155 {
156 public:
157 
158  microphone_wav(const std::string filepath)
159  : audio (filepath){}
160 
161  microphone_wav(std::ifstream & bytestream)
162  : audio (bytestream){}
163 
164  microphone_wav(std::vector<rapp::types::byte> bytearray)
165  : audio(bytearray)
166  {}
167 
168  std::string audio_source() const
169  { return "headset";}
170 
171  std::string extension() const
172  { return ".wav";}
173 };
174 }
175 }
176 #endif
OGG Class specialisation.
Definition: audio.hpp:103
std::string extension() const
Definition: audio.hpp:171
std::string extension() const
Definition: audio.hpp:150
void read_bytes(std::ifstream &bytestream)
Definition: audio.hpp:90
virtual std::string audio_source() const
Definition: audio.hpp:78
std::string audio_source() const
Definition: audio.hpp:168
bool operator==(const audio &rhs) const
Are audios same ?
Definition: audio.hpp:54
ogg(std::ifstream &bytestream)
Definition: audio.hpp:110
virtual std::string extension() const
Definition: audio.hpp:81
WAV Single channel 16Khz > Headset audio source.
Definition: audio.hpp:154
nao_quad_channel_wav(std::ifstream &bytestream)
Definition: audio.hpp:144
ogg(const std::string filepath)
Definition: audio.hpp:107
microphone_wav(std::ifstream &bytestream)
Definition: audio.hpp:161
nao_single_channel_wav(std::ifstream &bytestream)
Definition: audio.hpp:127
microphone_wav(std::vector< rapp::types::byte > bytearray)
Definition: audio.hpp:164
microphone_wav(const std::string filepath)
Definition: audio.hpp:158
class which wraps around raw bytes of an audiofile
Definition: audio.hpp:13
audio(std::ifstream &bytestream)
Construct using an open file stream.
Definition: audio.hpp:33
std::vector< rapp::types::byte > bytearray() const
Get audio as array of bytes.
Definition: audio.hpp:48
std::string extension() const
Definition: audio.hpp:133
std::string audio_source() const
Definition: audio.hpp:113
std::string audio_source() const
Definition: audio.hpp:147
WAV Class specialisation for quad channel.
Definition: audio.hpp:137
audio(std::vector< rapp::types::byte > bytearray)
construct using an existing byte-array
Definition: audio.hpp:40
bool save(const std::string filepath)
Save audio to filepath.
Definition: audio.hpp:64
audio(const std::string filepath)
Construct from a file on disk.
Definition: audio.hpp:17
audio & operator=(const audio &)=default
Assignment operator.
WAV Class specialisation for a single channel.
Definition: audio.hpp:120
nao_single_channel_wav(const std::string filepath)
Definition: audio.hpp:124
std::string extension() const
Definition: audio.hpp:116
nao_quad_channel_wav(const std::string filepath)
Definition: audio.hpp:141
std::vector< rapp::types::byte > bytearray_
Definition: audio.hpp:100
std::string audio_source() const
Definition: audio.hpp:130