audio.cpp 1.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "audio.h"

// --------------- STDLIB ---------------
#include <fstream>

// --------------- EXTERN ---------------
#include <util/files.h>
#include <util/log.h>

// --------------- INTERN ---------------


namespace audio
{
	wave_t loadWAV(const fs::path &path)
	{
		wave_t wave;
		wave.data_size = -1;

		std::ifstream wav_file(path, std::ios::binary | std::ios::in);

		if (files::readOne<uint32_t>(wav_file) != tag("RIFF"))
		{
			Log_Error << "WAV file not compatible: Missing RIFF tag";
			return wave;
		}

		files::readOne(wav_file, &wave.file_size);

		if (files::readOne<uint32_t>(wav_file) != tag("WAVE"))
		{
			Log_Error << "WAV file not compatible: Missing WAVE tag";
			return wave;
		}
		if (files::readOne<uint32_t>(wav_file) != tag("fmt "))
		{
			Log_Error << "WAV file not compatible: Missing format (fmt) tag";
			return wave;
		}

		files::readOne(wav_file, &wave.chunk_size);
		files::readOne(wav_file, &wave.format);
		files::readOne(wav_file, &wave.channels);
		files::readOne(wav_file, &wave.sample_rate);
		files::readOne(wav_file, &wave.avg_bytes_per_second);
		files::readOne(wav_file, &wave.bytes_per_sample);
		files::readOne(wav_file, &wave.bits_per_sample);

		if (files::readOne<uint32_t>(wav_file) != tag("data"))
		{
			Log_Error << "WAV file not compatible: Missing data tag";
			return wave;
		}

		files::readOne(wav_file, &wave.data_size);

		wave.data.resize(wave.data_size);
		wav_file.read(reinterpret_cast<char*>(wave.data.data()), wave.data.size() * sizeof(char));

		return wave;
	}
}