1 /* 2 * DSFML - The Simple and Fast Multimedia Library for D 3 * 4 * Copyright (c) 2013 - 2017 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 /** 26 * This class encodes audio samples to a sound file. 27 * 28 * It is used internally by higher-level classes such as $(SOUNDBUFFER_LINK), 29 * but can also be useful if you want to create audio files from custom data 30 * sources, like generated audio samples. 31 * 32 * Example: 33 * --- 34 * // Create a sound file, ogg/vorbis format, 44100 Hz, stereo 35 * auto file = new OutputSoundFile(); 36 * if (!file.openFromFile("music.ogg", 44100, 2)) 37 * { 38 * //error 39 * } 40 * 41 * while (...) 42 * { 43 * // Read or generate audio samples from your custom source 44 * short[] samples = ...; 45 * 46 * // Write them to the file 47 * file.write(samples); 48 * } 49 * --- 50 * 51 * See_Also: 52 * $(INPUTSOUNDFILE_LINK) 53 */ 54 module dsfml.audio.outputsoundfile; 55 56 import std..string; 57 import dsfml.system.err; 58 59 /** 60 * Provide write access to sound files. 61 */ 62 class OutputSoundFile 63 { 64 private sfOutputSoundFile* m_soundFile; 65 66 /// Default constructor. 67 this() 68 { 69 m_soundFile = sfOutputSoundFile_create(); 70 } 71 72 /// Destructor. 73 ~this() 74 { 75 import dsfml.system.config: destructorOutput; 76 mixin(destructorOutput); 77 sfOutputSoundFile_destroy(m_soundFile); 78 } 79 80 /** 81 * Open the sound file from the disk for writing. 82 * 83 * The supported audio formats are: WAV, OGG/Vorbis, FLAC. 84 * 85 * Params: 86 * filename = Path of the sound file to load 87 * sampleRate = Sample rate of the sound 88 * channelCount = Number of channels in the sound 89 * 90 * Returns: true if the file was successfully opened. 91 */ 92 bool openFromFile(const(char)[] filename, uint sampleRate, uint channelCount) 93 { 94 import dsfml.system..string; 95 bool toReturn = sfOutputSoundFile_openFromFile(m_soundFile, filename.ptr, filename.length,channelCount,sampleRate); 96 err.write(dsfml.system..string.toString(sfErr_getOutput())); 97 return toReturn; 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 extern(C) const(char)* sfErr_getOutput(); 114 115 116 private extern(C) 117 { 118 119 struct sfOutputSoundFile; 120 121 sfOutputSoundFile* sfOutputSoundFile_create(); 122 123 void sfOutputSoundFile_destroy(sfOutputSoundFile* file); 124 125 bool sfOutputSoundFile_openFromFile(sfOutputSoundFile* file, const(char)* filename, size_t length, uint channelCount,uint sampleRate); 126 127 void sfOutputSoundFile_write(sfOutputSoundFile* file, const short* data, long sampleCount); 128 129 }