1 module nudsfml.audio.soundbuffer;
2 
3 import std.string;
4 import bindbc.sfml.audio;
5 
6 import nudsfml.system.time;
7 import nudsfml.system.vector3;
8 
9 import nudsfml.audio.soundstatus;
10 
11 class SoundBuffer {
12     sfSoundBuffer* m_soundBuffer = null;
13 
14     this(const(sfSoundBuffer)* other){
15         m_soundBuffer = sfSoundBuffer_copy(other);
16     }
17 
18     this(SoundBuffer other) {
19         m_soundBuffer = sfSoundBuffer_copy(other.m_soundBuffer);
20     }
21     this(string filename){
22         createFromFile(filename);
23     }
24     this(void[] data){
25         createFromMemory(data);
26     }
27     ~this(){
28         if(m_soundBuffer !is null){
29             sfSoundBuffer_destroy(m_soundBuffer);
30         }
31     }
32     @property {
33         short[] samples() {
34             import core.stdc.string;
35             short* temp = cast(short*) sfSoundBuffer_getSamples(m_soundBuffer);
36             size_t length = sfSoundBuffer_getSampleCount(m_soundBuffer);
37 
38             short[] retval;
39             retval.length = length;
40             memcpy(temp, retval.ptr, length);
41 
42             return retval;
43         }
44     }
45     @property {
46         uint sampleRate(){
47             return sfSoundBuffer_getSampleRate(m_soundBuffer);
48         }
49     }
50     @property {
51         uint channelCount(){
52             return sfSoundBuffer_getChannelCount(m_soundBuffer);
53         }
54     }
55     @property {
56         Time duration(){
57             return cast(Time) sfSoundBuffer_getDuration(m_soundBuffer);
58         }
59     }
60 
61     bool createFromFile(string filename){
62         if(m_soundBuffer !is null){
63             sfSoundBuffer_destroy(m_soundBuffer);
64         }
65         m_soundBuffer = sfSoundBuffer_createFromFile(filename.toStringz);
66         return m_soundBuffer !is null;
67     }
68     bool createFromMemory(void[] data){
69         if(m_soundBuffer !is null){
70             sfSoundBuffer_destroy(m_soundBuffer);
71         }
72         m_soundBuffer = sfSoundBuffer_createFromMemory(data.ptr, data.length);
73         return m_soundBuffer !is null;
74     }
75 }