File: Read.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (92 lines) | stat: -rw-r--r-- 2,210 bytes parent folder | download | duplicates (3)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "stdafx.h"
#include "Read.h"
#include "OggSound.h"
#include "FlacSound.h"
#include "WavSound.h"
#include "Mp3Sound.h"
#include "Exception.h"

namespace sound {

	// check the header in the stream
	// zeroTerm - Zero-terminated string in the file?
	static bool checkHeader(IStream *file, const char *header, bool zeroTerm) {
		nat len = nat(strlen(header));
		if (zeroTerm)
			len++;

		storm::Buffer buffer = file->peek(storm::buffer(file->engine(), len));
		if (!buffer.full()) {
			return false;
		}

		for (nat i = 0; i < len; i++) {
			if (byte(buffer[i]) != byte(header[i])) {
				return false;
			}
		}

		return true;
	}


	Sound *sound(IStream *src) {
		Sound *result = null;

		if (checkHeader(src, "OggS", false)) {
			result = openOgg(src->randomAccess());
		} else if (checkHeader(src, "fLaC", false)) {
			result = openFlac(src->randomAccess());
		} else if (checkHeader(src, "RIFF", false)) {
			result = openWav(src->randomAccess());
		} else if (checkHeader(src, "ID3", false)) {
			result = openMp3(src->randomAccess());
		} else if (checkHeader(src, "\xFF\xFB", false)) {
			result = openMp3(src->randomAccess());
		} else {
			throw new (src) SoundOpenError(S("Unknown file format."));
		}

		if (!result)
			throw new (src) SoundOpenError(S("Failed to open file."));

		return result;
	}

	Sound *soundStream(IStream *src) {
		Sound *result = null;

		const char *foo = "\xFF\xFB";
		PVAR(strlen("\xFF\xFB"));
		PVAR(int(foo[0]));
		PVAR(int(foo[1]));

		if (checkHeader(src, "OggS", false)) {
			result = openOggStream(src);
		} else if (checkHeader(src, "fLaC", false)) {
			result = openFlac(src->randomAccess());
		} else if (checkHeader(src, "RIFF", false)) {
			result = openWavStream(src);
		} else if (checkHeader(src, "ID3", false)) {
			result = openMp3Stream(src);
		} else if (checkHeader(src, "\xFF\xFB", false)) {
			result = openMp3Stream(src);
		} else {
			throw new (src) SoundOpenError(S("Unknown file format."));
		}

		if (!result)
			throw new (src) SoundOpenError(S("Failed to open file."));

		return result;
	}

	Sound *readSound(Url *file) {
		return sound(file->read());
	}

	Sound *readSoundStream(Url *file) {
		return soundStream(file->read());
	}

}