Template repo for tiny cross-platform apps that can be modified on phone, tablet or computer.
at main 160 lines 6.6 kB view raw
1local wav = { 2 --[[ 3 Writes audio file in WAVE format with PCM integer samples. 4 5 Function 'create_context' requires a filename and returns a table with the 6 following methods: 7 - init(channels_number, sample_rate, bits_per_sample) 8 - write_samples_interlaced(samples) 9 - finish() 10 11 (WAVE format: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/) 12 ]] 13 --[[ 14 Copyright © 2014, Christoph "Youka" Spanknebel 15 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 16 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 18 ]] 19 create_context = function(filename) 20 -- Check function parameters 21 if type(filename) ~= "string" then 22 error("invalid function parameter, expected string filename", 2) 23 end 24 local absolute_path = love.filesystem.getSaveDirectory()..'/'..filename 25 -- ensure directory exists 26 love.filesystem.write(absolute_path, '') 27 -- Audio file handle 28 local file = io.open(absolute_path, "wb") 29 if not file then 30 error(string.format("couldn't open file %q", filename), 2) 31 end 32 -- Byte-string(unsigned integer,little endian)<-Lua-number converter 33 local function ntob(n, len) 34 local n, bytes = math.max(math.floor(n), 0), {} 35 for i=1, len do 36 bytes[i] = n % 256 37 n = math.floor(n / 256) 38 end 39 return string.char(unpack(bytes)) 40 end 41 -- Check for integer 42 local function isint(n) 43 return type(n) == "number" and n == math.floor(n) 44 end 45 -- Audio meta informations 46 local channels_number_private, bytes_per_sample 47 -- Return audio handler 48 return { 49 init = function(channels_number, sample_rate, bits_per_sample) 50 -- Check function parameters 51 if not isint(channels_number) or channels_number < 1 or 52 not isint(sample_rate) or sample_rate < 2 or 53 not (bits_per_sample == 8 or bits_per_sample == 16 or bits_per_sample == 24 or bits_per_sample == 32) then 54 error("valid channels number, sample rate and bits per sample expected", 2) 55 end 56 -- Write file type 57 file:write("RIFF????WAVE") -- file size to insert later 58 -- Write format chunk 59 file:write("fmt ", 60 ntob(16, 4), 61 ntob(1, 2), 62 ntob(channels_number, 2), 63 ntob(sample_rate, 4), 64 ntob(sample_rate * channels_number * (bits_per_sample / 8), 4), 65 ntob(channels_number * (bits_per_sample / 8), 2), 66 ntob(bits_per_sample, 2)) 67 -- Write data chunk (so far) 68 file:write("data????") -- data size to insert later 69 -- Set format memory 70 channels_number_private, bytes_per_sample = channels_number, bits_per_sample / 8 71 end, 72 write_samples_interlaced = function(samples) 73 -- Check function parameters 74 if type(samples) ~= "table" then 75 error("samples table expected", 2) 76 end 77 local samples_n = #samples 78 if samples_n == 0 or samples_n % channels_number_private ~= 0 then 79 error("valid number of samples expected (multiple of channels)", 2) 80 -- Already finished? 81 elseif not file then 82 error("already finished", 2) 83 -- Already initialized? 84 elseif file:seek() == 0 then 85 error("initialize before writing samples", 2) 86 end 87 -- All samples are numbers? 88 for i=1, samples_n do 89 if type(samples[i]) ~= "number" then 90 error("samples have to be numbers", 2) 91 end 92 end 93 -- Write samples to file 94 local sample 95 if bytes_per_sample == 1 then 96 for i=1, samples_n do 97 sample = samples[i] 98 file:write(ntob(sample < 0 and sample + 256 or sample, 1)) 99 end 100 elseif bytes_per_sample == 2 then 101 for i=1, samples_n do 102 sample = samples[i] 103 file:write(ntob(sample < 0 and sample + 65536 or sample, 2)) 104 end 105 elseif bytes_per_sample == 3 then 106 for i=1, samples_n do 107 sample = samples[i] 108 file:write(ntob(sample < 0 and sample + 16777216 or sample, 3)) 109 end 110 else -- if bytes_per_sample == 4 then 111 for i=1, samples_n do 112 sample = samples[i] 113 file:write(ntob(sample < 0 and sample + 4294967296 or sample, 4)) 114 end 115 end 116 end, 117 finish = function() 118 -- Already finished? 119 if not file then 120 error("already finished", 2) 121 -- Already initialized? 122 elseif file:seek() == 0 then 123 error("initialize before finishing", 2) 124 end 125 -- Get file size 126 local file_size = file:seek() 127 -- Save file size 128 file:seek("set", 4) 129 file:write(ntob(file_size - 8, 4)) 130 -- Save data size 131 file:seek("set", 40) 132 file:write(ntob(file_size - 44, 4)) 133 -- Finalize file for secure reading 134 file:close() 135 file = nil 136 end 137 } 138 end, 139} 140 141function save_wav(filename, sounddata) 142 -- credit to Intas and zorg on the LÖVE forum <3 143 -- https://love2d.org/forums/viewtopic.php?t=86173 144 local samples = {} 145 for i = 0, sounddata:getSampleCount() - 1 do 146 local sample = sounddata:getSample(i) 147 local n 148 local to16bit = sample * 32767 149 if (to16bit > 0) then 150 n = math.floor(math.min(to16bit, 32767)) 151 else 152 n = math.floor(math.max(to16bit, -32768)) 153 end 154 table.insert(samples, n) 155 end 156 local w = wav.create_context(filename, "w") 157 w.init(sounddata:getChannelCount(), sounddata:getSampleRate(), sounddata:getBitDepth()) 158 w.write_samples_interlaced(samples) 159 w.finish() 160end