1 /**
2  * ae.ui.audio.memory.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.memory;
15 
16 import std.algorithm.comparison;
17 import std.algorithm.mutation;
18 import std.range;
19 import std.range.primitives;
20 
21 import ae.ui.audio.source.base;
22 
23 template MemorySoundSource(Sample)
24 {
25 	final class MemorySoundSource : AbstractSoundSource!Sample
26 	{
27 		Sample[] samples;
28 		uint sampleRate;
29 
30 		this(Sample[] samples, uint sampleRate)
31 		{
32 			this.samples = samples;
33 			this.sampleRate = sampleRate;
34 		}
35 
36 		override uint getSampleRate() const nothrow
37 		{
38 			return sampleRate;
39 		}
40 
41 		override size_t getNumChannels() const nothrow
42 		{
43 			// TODO
44 			return 1;
45 		}
46 
47 		override bool procedural() const nothrow
48 		{
49 			return false;
50 		}
51 
52 		override size_t copySamples(size_t channel, size_t start, Sample[] buffer) const nothrow
53 		{
54 			auto slice = getSamples(channel, start, buffer.length);
55 			buffer[0 .. slice.length] = slice;
56 			return slice.length;
57 		}
58 
59 		override const(Sample)[] getSamples(size_t channel, size_t start, size_t maxLength) const nothrow
60 		{
61 			start = min(start, samples.length);
62 			auto end = min(start + maxLength, samples.length);
63 			return samples[start .. end];
64 		}
65 	}
66 }
67 
68 MemorySoundSource!Sample memorySoundSource(Sample)(Sample[] samples, uint sampleRate)
69 {
70 	return new MemorySoundSource!Sample(samples, sampleRate);
71 }
72 
73 unittest
74 {
75 	auto w = memorySoundSource([short.max, short.min], 44100);
76 }