File: WavSound.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 (156 lines) | stat: -rw-r--r-- 3,213 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "stdafx.h"
#include "WavSound.h"
#include "Exception.h"

namespace sound {

	struct RiffChunk {
		char header[4];
		nat size;
		char format[4];
	};

	struct FmtChunk {
		char chunkId[4];
		nat size;
		unsigned short audioFmt;
		unsigned short numCh;
		nat rate;
		nat byteRate;
		unsigned short blockAlign;
		unsigned short bitsPerSample;
	};

	struct DataChunk {
		char chunkId[4];
		nat size;
	};

	template <class T>
	void fill(IStream *from, T &out) {
		GcPreArray<byte, sizeof(T)> data;
		storm::Buffer b = from->fill(storm::emptyBuffer(data));
		if (!b.full())
			throw new (from) SoundOpenError(S("Not enough data."));

		memcpy(&out, b.dataPtr(), sizeof(T));
	}

	WavSound::WavSound(IStream *src, Bool seekable) : src(src), seekable(seekable) {
		RiffChunk riff;
		fill(src, riff);

		if (strncmp(riff.header, "RIFF", 4) != 0)
			throw new (this) SoundOpenError(S("Invalid header."));
		if (strncmp(riff.format, "WAVE", 4) != 0)
			throw new (this) SoundOpenError(S("Unsupported wave format."));

		FmtChunk fmt;
		fill(src, fmt);

		if (strncmp(fmt.chunkId, "fmt ", 4) != 0)
			throw new (this) SoundOpenError(S("Invalid subchunk."));
		if (fmt.size != 16)
			throw new (this) SoundOpenError(S("Invalid chunk size."));
		if (fmt.audioFmt != 1)
			throw new (this) SoundOpenError(S("Compression in wave files is not supported."));

		rate = fmt.rate;
		ch = fmt.numCh;
		sampleDepth = fmt.bitsPerSample;
		sampleSize = fmt.blockAlign;

		if (sampleDepth != 8 && sampleDepth != 16)
			throw new (this) SoundOpenError(S("Unsupported bit depth."));

		DataChunk data;
		fill(src, data);

		if (strncmp(data.chunkId, "data", 4) != 0)
			throw new (this) SoundOpenError(S("Invalid data chunk."));
		if (seekable)
			dataStart = ((RIStream *)src)->tell();
		dataLength = data.size / sampleSize;
	}

	void WavSound::close() {
		if (src)
			src->close();
		src = null;
	}

	Word WavSound::tell() {
		if (seekable)
			return dataPos;
		else
			return 0;
	}

	Bool WavSound::seek(Word to) {
		if (!seekable)
			return false;

		dataPos = max(dataLength, to);
		RIStream *s = (RIStream *)src;
		s->seek(dataStart + dataPos*sampleSize);
		return true;
	}

	Word WavSound::length() {
		if (seekable)
			return dataLength;
		else
			return 0;
	}

	Nat WavSound::sampleFreq() const {
		return rate;
	}

	Nat WavSound::channels() const {
		return ch;
	}

	Buffer WavSound::read(Buffer to) {
		Nat free = to.count() - to.filled();
		Nat samples = free / ch;
		dataPos = min(dataLength, dataPos + samples);

		storm::Buffer src = this->src->fill(samples * sampleSize);
		Nat out = to.filled();
		switch (sampleDepth) {
		case 8:
			for (Nat i = 0; i < src.filled(); i++) {
				to[out++] = (src[i]/128.0f) - 1.0f;
			}
			break;
		case 16:
			for (Nat i = 0; i < src.filled(); i += 2) {
				short data = src[i];
				data |= short(src[i+1]) << 8;
				to[out++] = float(data)/float(0x8000);
			}
			break;
		default:
			out = to.count();
			break;
		}

		to.filled(out);
		return to;
	}

	Bool WavSound::more() {
		return dataPos < dataLength;
	}


	WavSound *openWav(RIStream *src) {
		return new (src) WavSound(src, true);
	}

	WavSound *openWavStream(IStream *src) {
		return new (src) WavSound(src, false);
	}

}