1 /*
2  * DSFML - The Simple and Fast Multimedia Library for D
3  *
4  * Copyright (c) 2013 - 2018 Jeremy DeHaan (dehaan.jeremiah@gmail.com)
5  *
6  * This software is provided 'as-is', without any express or implied warranty.
7  * In no event will the authors be held liable for any damages arising from the
8  * use of this software.
9  *
10  * Permission is granted to anyone to use this software for any purpose,
11  * including commercial applications, and to alter it and redistribute it
12  * freely, subject to the following restrictions:
13  *
14  * 1. The origin of this software must not be misrepresented; you must not claim
15  * that you wrote the original software. If you use this software in a product,
16  * an acknowledgment in the product documentation would be appreciated but is
17  * not required.
18  *
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  * misrepresented as being the original software.
21  *
22  * 3. This notice may not be removed or altered from any source distribution
23  *
24  *
25  * DSFML is based on SFML (Copyright Laurent Gomila)
26  */
27 
28 /**
29   * This class encodes audio samples to a sound file.
30  *
31  * It is used internally by higher-level classes such as $(SOUNDBUFFER_LINK),
32  * but can also be useful if you want to create audio files from custom data
33  * sources, like generated audio samples.
34  *
35  * Example:
36  * ---
37  * // Create a sound file, ogg/vorbis format, 44100 Hz, stereo
38  * auto file = new OutputSoundFile();
39  * if (!file.openFromFile("music.ogg", 44100, 2))
40  * {
41  *     //error
42  * }
43  *
44  * while (...)
45  * {
46  *     // Read or generate audio samples from your custom source
47  *     short[] samples = ...;
48  *
49  *     // Write them to the file
50  *     file.write(samples);
51  * }
52  * ---
53  *
54  * See_Also:
55  * $(INPUTSOUNDFILE_LINK)
56  */
57 module dsfml.audio.outputsoundfile;
58 
59 import std..string;
60 import dsfml.system.err;
61 
62 /**
63  * Provide write access to sound files.
64  */
65 class OutputSoundFile
66 {
67     private sfOutputSoundFile* m_soundFile;
68 
69     /// Default constructor.
70     this()
71     {
72         m_soundFile = sfOutputSoundFile_create();
73     }
74 
75     /// Destructor.
76     ~this()
77     {
78         import dsfml.system.config: destructorOutput;
79         mixin(destructorOutput);
80         sfOutputSoundFile_destroy(m_soundFile);
81     }
82 
83     /**
84      * Open the sound file from the disk for writing.
85      *
86      * The supported audio formats are: WAV, OGG/Vorbis, FLAC.
87      *
88      * Params:
89      *	filename     = Path of the sound file to load
90      *	sampleRate   = Sample rate of the sound
91      *	channelCount = Number of channels in the sound
92      *
93      * Returns: true if the file was successfully opened.
94      */
95     bool openFromFile(const(char)[] filename, uint sampleRate, uint channelCount)
96     {
97         return  sfOutputSoundFile_openFromFile(m_soundFile, filename.ptr, filename.length,channelCount,sampleRate);
98     }
99 
100     /**
101      * Write audio samples to the file.
102      *
103      * Params:
104      *	samples = array of samples to write
105      */
106     void write(const(short)[] samples)
107     {
108         sfOutputSoundFile_write(m_soundFile, samples.ptr, samples.length);
109     }
110 
111 }
112 
113 private extern(C)
114 {
115 
116 struct sfOutputSoundFile;
117 
118 sfOutputSoundFile* sfOutputSoundFile_create();
119 
120 void sfOutputSoundFile_destroy(sfOutputSoundFile* file);
121 
122 bool sfOutputSoundFile_openFromFile(sfOutputSoundFile* file, const(char)* filename, size_t length, uint channelCount,uint sampleRate);
123 
124 void sfOutputSoundFile_write(sfOutputSoundFile* file, const short* data, long sampleCount);
125 
126 }