fork download
  1. // .cpp file
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <string>
  6. #include <vector>
  7. #include <inttypes.h>
  8. #include <stdint.h>
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "Wav.h"
  14. #include "types.h"
  15.  
  16. using namespace std;
  17.  
  18. uint32_t size = 0;
  19. int bits_per_sample;
  20. int sample_rate2;
  21.  
  22. AudioLib::Wav::Wav() {
  23.  
  24. }
  25.  
  26. AudioLib::Wav::Wav(const int N, const int M) : AudioLib::Signal() {
  27.  
  28. /*TO DO TO DO TO DO TO DO*/
  29. }
  30.  
  31. AudioLib::Wav::Wav(string theFile, ReadType type) {
  32.  
  33. ifstream file (theFile.c_str());
  34.  
  35. if(!this->_checkFileExists(file))
  36. {
  37. cerr << " :: Cannot Read (WAV) File ::";
  38. exit(0);
  39. }
  40.  
  41. if(!this->readWav(file, type))
  42. {
  43. cerr << "An error occured";
  44. exit(0);
  45. }
  46.  
  47. }
  48.  
  49. vector<double> const& AudioLib::Wav::Signal() const {
  50.  
  51. return this->rawSignal;
  52. }
  53.  
  54. inline bool AudioLib::Wav::readHeader(ifstream& file) {
  55.  
  56. s_riff_hdr riff_hdr;
  57. s_chunk_hdr chunk_hdr;
  58.  
  59. long padded_size; // Size of extra bits
  60.  
  61. vector<uint8_t> fmt_data; // Vector to store the FMT data.
  62.  
  63. s_wavefmt *fmt = NULL;
  64.  
  65. file.read(reinterpret_cast<char*>(&riff_hdr), sizeof(riff_hdr));
  66. if (!file) return false;
  67.  
  68. if (memcmp(riff_hdr.id, "RIFF", 4) != 0) return false;
  69.  
  70. //cout << "size=" << riff_hdr.size << endl;
  71. //cout << "type=" << string(riff_hdr.type, 4) << endl;
  72.  
  73. if (memcmp(riff_hdr.type, "WAVE", 4) != 0) return false;
  74. {
  75. do
  76. {
  77. file.read(reinterpret_cast<char*>(&chunk_hdr), sizeof(chunk_hdr));
  78. if (!file) return false;
  79. padded_size = ((chunk_hdr.size + 1) & ~1);
  80.  
  81. if (memcmp(chunk_hdr.id, "fmt ", 4) == 0)
  82. {
  83. if (chunk_hdr.size < sizeof(s_wavefmt)) return false;
  84.  
  85. fmt_data.resize(padded_size);
  86. file.read(reinterpret_cast<char*>(&fmt_data[0]), padded_size);
  87. if (!file) return false;
  88.  
  89. fmt = reinterpret_cast<s_wavefmt*>(&fmt_data[0]);
  90.  
  91. sample_rate2 = fmt->sample_rate;
  92.  
  93. if (fmt->format_tag == 1) // PCM
  94. {
  95. if (chunk_hdr.size < sizeof(s_pcmwavefmt)) return false;
  96.  
  97. s_pcmwavefmt *pcm_fmt = reinterpret_cast<s_pcmwavefmt*>(fmt);
  98.  
  99.  
  100. bits_per_sample = pcm_fmt->bits_per_sample;
  101. }
  102. else
  103. {
  104. if (chunk_hdr.size < sizeof(s_wavefmtex)) return false;
  105.  
  106. s_wavefmtex *fmt_ex = reinterpret_cast<s_wavefmtex*>(fmt);
  107.  
  108.  
  109. if (fmt_ex->extra_size != 0)
  110. {
  111. if (chunk_hdr.size < (sizeof(s_wavefmtex) + fmt_ex->extra_size)) return false;
  112.  
  113. uint8_t *extra_data = reinterpret_cast<uint8_t*>(fmt_ex + 1);
  114. // use extra_data, up to extra_size bytes, as needed...
  115. }
  116.  
  117. }
  118. //cout << "extra_size=" << fmt_ex->extra_size << endl;
  119. }
  120.  
  121. else if (memcmp(chunk_hdr.id, "data", 4) == 0)
  122. {
  123. // process chunk data, according to fmt, as needed...
  124. size = padded_size;
  125.  
  126. if(bits_per_sample == 16)
  127. {
  128. //size = padded_size / 2;
  129. }
  130.  
  131. data = new char[size];
  132.  
  133. file.read(data, size);
  134.  
  135. file.ignore(padded_size);
  136. if (!file) return false;
  137. }
  138. else
  139. {
  140. // process other chunks as needed...
  141.  
  142. file.ignore(padded_size);
  143. if (!file) return false;
  144. }
  145.  
  146. }while (!file.eof());
  147. return true;
  148. }
  149.  
  150. }
  151.  
  152. inline bool AudioLib::Wav::readWav(ifstream &file, ReadType type) {
  153.  
  154. if(!this->readHeader(file))
  155. {
  156. return 0;
  157. }
  158.  
  159. IntN< 16>::type x;
  160.  
  161. switch(type)
  162. {
  163. case NATIVE:
  164.  
  165. /* to do to do to do to do to do to do */
  166.  
  167.  
  168. break;
  169.  
  170. case DOUBLE:
  171.  
  172. sample_rate = sample_rate2;
  173.  
  174. if(bits_per_sample == 8)
  175. {
  176. uint8_t c;
  177. //cout << size;
  178. for(unsigned i=0; (i < size); i++)
  179. {
  180. c = (unsigned)(unsigned char)(data[i]);
  181. double t = (c-128)/128.0;
  182. rawSignal.push_back(t);
  183. }
  184. }
  185. else if(bits_per_sample == 16)
  186. {
  187.  
  188. for (int i = 0; i < size; i += 2)
  189. {
  190. int c = (data[i + 1] << 8) | data[i];
  191. double t = c/32768.0;
  192. //cout << t << endl;
  193. rawSignal.push_back(t);
  194. }
  195. }else{
  196. cerr << "Cannot Initalise correct 'bits_per_sample' in reference to function: 'readWav' line: 67";
  197.  
  198. }
  199. break;
  200.  
  201. default: return false; break;
  202.  
  203. }
  204.  
  205. return true;
  206. }
  207. AudioLib::Wav& AudioLib::Wav::Emphasize()
  208. {
  209. Signal::Emphasize();
  210. return *this;
  211. }
  212.  
  213. AudioLib::Wav& AudioLib::Wav::Compress()
  214. {
  215. Signal::Compress();
  216. return *this;
  217.  
  218. }
  219. int AudioLib::Wav::getSampleRate() {
  220.  
  221. return this->sample_rate;
  222. }
  223. inline bool AudioLib::Wav::_checkFileExists(ifstream &file) {
  224.  
  225. return true;
  226.  
  227. }
  228.  
  229. // wav.h
  230.  
  231. #include <iostream>
  232. #include <vector>
  233. #include <fstream>
  234. #include "types.h"
  235. #include "../Signal/Signal.h"
  236. using namespace std;
  237.  
  238. #ifndef _Wav_h
  239. #define _Wav_h
  240.  
  241. enum ReadType {
  242.  
  243. NATIVE = 0, DOUBLE,
  244. };
  245.  
  246. namespace AudioLib {
  247.  
  248. class Wav : public Signal {
  249.  
  250. public:
  251. Wav();
  252. Wav(const int N, const int M);
  253. Wav(string theFile, ReadType type);
  254. vector<double> const& Signal() const;
  255.  
  256. inline bool readHeader(ifstream& file);
  257. inline bool readWav(ifstream &file, ReadType type);
  258. inline bool _checkFileExists(ifstream &file);
  259. int getSampleRate();
  260. Wav& Emphasize();
  261. Wav& Compress();
  262. private:
  263.  
  264. #pragma pack(push, 1)
  265. struct s_riff_hdr
  266. {
  267. char id[4];
  268. uint32_t size;
  269. char type[4];
  270. };
  271.  
  272. struct s_chunk_hdr
  273. {
  274. char id[4];
  275. uint32_t size;
  276. };
  277.  
  278. struct s_wavefmt
  279. {
  280. uint16_t format_tag;
  281. uint16_t channels;
  282. uint32_t sample_rate;
  283. uint32_t avg_bytes_sec;
  284. uint16_t block_align;
  285. };
  286.  
  287. struct s_wavefmtex
  288. {
  289. s_wavefmt fmt;
  290. uint16_t bits_per_sample;
  291. uint16_t extra_size;
  292. };
  293.  
  294. struct s_pcmwavefmt
  295. {
  296. s_wavefmt fmt;
  297. uint16_t bits_per_sample;
  298. };
  299.  
  300. #pragma pack(pop)
  301. char* data;
  302. int sample_rate;
  303.  
  304. };
  305.  
  306. }
  307.  
  308. #endif
  309.  
  310. // Makefile
  311. # Location of the Python Header files
  312. # This is system dependant.
  313.  
  314. PYTHON_VERSION = 2.7
  315. PYTHON_INCLUDE = /usr/include/python$(PYTHON_VERSION)
  316.  
  317. CFLAGS += -O3
  318. CFLAGS += -std=c++11
  319. CFLAGS += -pg -D_DEBUG -g -c -Wall
  320. # location of the Boost Python include files and library
  321.  
  322. BOOST_INC = /usr/include
  323. BOOST_LIB = /usr/lib
  324.  
  325. WAV_TARGET =
  326. WAV_SOURCE = n
  327.  
  328. # Compile the .wav Python and Cpp file
  329. $(WAV_TARGET).so: $(WAV_TARGET).o
  330. g++ -shared -Wl,--export-dynamic $(WAV_TARGET).o -L$(BOOST_LIB) -lboost_python -lboost_python -L/usr/lib/python$(PYTHON_VERSION)/config -lpython$(PYTHON_VERSION) -o $(WAV_TARGET).so
  331.  
  332. $(WAV_TARGET).o: $(WAV_TARGET).hpp
  333. g++ $(CFLAGS) -I$(PYTHON_INCLUDE) -I$(BOOST_INC) -fPIC -c $(WAV_TARGET).cpp
  334.  
  335.  
  336.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:13:17: fatal error: Wav.h: No such file or directory
 #include "Wav.h"
                 ^
compilation terminated.
stdout
Standard output is empty