Fireworks Engine  v2.0
Lightweight Sandbox Game Engine using OpenGL for additional Customisation and Quick Prototyping
wavloader.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <fstream>
5 #include <cstring>
6 #include <al.h>
7 #include <bit>
8 
9 namespace fireworks { namespace utils {
10 
12  class WavLoader
13  {
14  public:
15  bool isBigEndian()
16  {
17  int a = 1;
18  return !((char*)&a)[0];
19  }
20 
21  int convertToInt(char* buffer, std::uint32_t len)
22  {
23  std::uint32_t a = 0;
24  if (!isBigEndian())
25  for (std::uint32_t i = 0; i < len; i++)
26  ((char*)&a)[i] = buffer[i];
27  else
28  for (std::uint32_t i = 0; i < len; i++)
29  ((char*)&a)[3 - i] = buffer[i];
30  return a;
31  }
32 
34  char* loadWAV(const char* fn, std::uint32_t& format, std::uint32_t& samplerate, std::uint32_t& bps, std::uint32_t& size)
35  {
36  std::uint32_t channels;
37  char buffer[4];
38  std::ifstream in(fn, std::ios::binary);
39  in.read(buffer, 4);
40  if (strncmp(buffer, "RIFF", 4) != 0)
41  {
42  std::cout << "this is not a valid WAVE file" << std::endl;
43  return NULL;
44  }
45  in.read(buffer, 4);
46  in.read(buffer, 4); //WAVE
47  in.read(buffer, 4); //fmt
48  in.read(buffer, 4); //16
49  in.read(buffer, 2); //1
50  in.read(buffer, 2);
51  channels = convertToInt(buffer, 2);
52  in.read(buffer, 4);
53  samplerate = convertToInt(buffer, 4);
54  in.read(buffer, 4);
55  in.read(buffer, 2);
56  in.read(buffer, 2);
57  bps = convertToInt(buffer, 2);
58  in.read(buffer, 4); //data
59  in.read(buffer, 4);
60  size = convertToInt(buffer, 4);
61  char* data = new char[size];
62  in.read(data, size);
63 
64  if (channels == 1)
65  {
66  if (bps == 8)
67  {
68  format = AL_FORMAT_MONO8;
69  }
70  else {
71  format = AL_FORMAT_MONO16;
72  }
73  }
74  else {
75  if (bps == 8)
76  {
77  format = AL_FORMAT_STEREO8;
78  }
79  else {
80  format = AL_FORMAT_STEREO16;
81  }
82  }
83  return data;
84  }
85  };
86 
87 } }
Definition: audioclip.cpp:3
char * loadWAV(const char *fn, std::uint32_t &format, std::uint32_t &samplerate, std::uint32_t &bps, std::uint32_t &size)
Loads the wav file.
Definition: wavloader.h:34
int convertToInt(char *buffer, std::uint32_t len)
Definition: wavloader.h:21
A class to load WAVE (.wav) audio files.
Definition: wavloader.h:12
bool isBigEndian()
Definition: wavloader.h:15