1 /**
2  * ae.ui.audio.wave.base
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.ui.audio.source.wave;
15 
16 import std.algorithm.mutation;
17 import std.range;
18 import std.range.primitives;
19 
20 import ae.ui.audio.source.base;
21 
22 template WaveSoundSource(Wave)
23 {
24 	alias Sample = typeof(Wave.init.front);
25 
26 	class WaveSoundSource : AbstractSoundSource!Sample
27 	{
28 		Wave wave;
29 		uint sampleRate;
30 
31 		this(Wave wave, uint sampleRate)
32 		{
33 			this.wave = wave;
34 			this.sampleRate = sampleRate;
35 		}
36 
37 		override uint getSampleRate() const nothrow
38 		{
39 			return sampleRate;
40 		}
41 
42 		override size_t getNumChannels() const nothrow
43 		{
44 			// TODO
45 			return 1;
46 		}
47 
48 		override bool procedural() const nothrow
49 		{
50 			return true;
51 		}
52 
53 		override size_t copySamples(size_t channel, size_t start, Sample[] buffer) const nothrow
54 		{
55 			auto w = cast(Wave)wave; // Break constness because Map.save is not const
56 			auto remaining = copy(w.drop(start).take(buffer.length), buffer);
57 			return buffer.length - remaining.length;
58 		}
59 
60 		override const(Sample)[] getSamples(size_t channel, size_t start, size_t maxLength) const nothrow
61 		{
62 			assert(false, "Procedural");
63 		}
64 	}
65 }
66 
67 WaveSoundSource!Wave waveSoundSource(Wave)(Wave wave, uint sampleRate)
68 {
69 	return new WaveSoundSource!Wave(wave, sampleRate);
70 }
71 
72 unittest
73 {
74 	auto w = waveSoundSource([short.max, short.min], 44100);
75 }