Template repo for tiny cross-platform apps that can be modified on phone, tablet or computer.
local wav = {
  --[[
    Writes audio file in WAVE format with PCM integer samples.

    Function 'create_context' requires a filename and returns a table with the
    following methods:
    - init(channels_number, sample_rate, bits_per_sample)
    - write_samples_interlaced(samples)
    - finish()

    (WAVE format: https://ccrma.stanford.edu/courses/422/projects/WaveFormat/)
  ]]
  --[[
    Copyright © 2014, Christoph "Youka" Spanknebel
    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:
    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
    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.
  ]]
  create_context = function(filename)
    -- Check function parameters
    if type(filename) ~= "string" then
      error("invalid function parameter, expected string filename", 2)
    end
    local absolute_path = love.filesystem.getSaveDirectory()..'/'..filename
    -- ensure directory exists
    love.filesystem.write(absolute_path, '')
    -- Audio file handle
    local file = io.open(absolute_path, "wb")
    if not file then
      error(string.format("couldn't open file %q", filename), 2)
    end
    -- Byte-string(unsigned integer,little endian)<-Lua-number converter
    local function ntob(n, len)
      local n, bytes = math.max(math.floor(n), 0), {}
      for i=1, len do
        bytes[i] = n % 256
        n = math.floor(n / 256)
      end
      return string.char(unpack(bytes))
    end
    -- Check for integer
    local function isint(n)
      return type(n) == "number" and n == math.floor(n)
    end
    -- Audio meta informations
    local channels_number_private, bytes_per_sample
    -- Return audio handler
    return {
      init = function(channels_number, sample_rate, bits_per_sample)
        -- Check function parameters
        if not isint(channels_number) or channels_number < 1 or
          not isint(sample_rate) or sample_rate < 2 or
          not (bits_per_sample == 8 or bits_per_sample == 16 or bits_per_sample == 24 or bits_per_sample == 32) then
          error("valid channels number, sample rate and bits per sample expected", 2)
        end
        -- Write file type
        file:write("RIFF????WAVE")  -- file size to insert later
        -- Write format chunk
        file:write("fmt ",
              ntob(16, 4),
              ntob(1, 2),
              ntob(channels_number, 2),
              ntob(sample_rate, 4),
              ntob(sample_rate * channels_number * (bits_per_sample / 8), 4),
              ntob(channels_number * (bits_per_sample / 8), 2),
              ntob(bits_per_sample, 2))
        -- Write data chunk (so far)
        file:write("data????")  -- data size to insert later
        -- Set format memory
        channels_number_private, bytes_per_sample = channels_number, bits_per_sample / 8
      end,
      write_samples_interlaced = function(samples)
        -- Check function parameters
        if type(samples) ~= "table" then
          error("samples table expected", 2)
        end
        local samples_n = #samples
        if samples_n == 0 or samples_n % channels_number_private ~= 0 then
          error("valid number of samples expected (multiple of channels)", 2)
        -- Already finished?
        elseif not file then
          error("already finished", 2)
        -- Already initialized?
        elseif file:seek() == 0 then
          error("initialize before writing samples", 2)
        end
        -- All samples are numbers?
        for i=1, samples_n do
          if type(samples[i]) ~= "number" then
            error("samples have to be numbers", 2)
          end
        end
        -- Write samples to file
        local sample
        if bytes_per_sample == 1 then
          for i=1, samples_n do
            sample = samples[i]
            file:write(ntob(sample < 0 and sample + 256 or sample, 1))
          end
        elseif bytes_per_sample == 2 then
          for i=1, samples_n do
            sample = samples[i]
            file:write(ntob(sample < 0 and sample + 65536 or sample, 2))
          end
        elseif bytes_per_sample == 3 then
          for i=1, samples_n do
            sample = samples[i]
            file:write(ntob(sample < 0 and sample + 16777216 or sample, 3))
          end
        else  -- if bytes_per_sample == 4 then
          for i=1, samples_n do
            sample = samples[i]
            file:write(ntob(sample < 0 and sample + 4294967296 or sample, 4))
          end
        end
      end,
      finish = function()
        -- Already finished?
        if not file then
          error("already finished", 2)
        -- Already initialized?
        elseif file:seek() == 0 then
          error("initialize before finishing", 2)
        end
        -- Get file size
        local file_size = file:seek()
        -- Save file size
        file:seek("set", 4)
        file:write(ntob(file_size - 8, 4))
        -- Save data size
        file:seek("set", 40)
        file:write(ntob(file_size - 44, 4))
        -- Finalize file for secure reading
        file:close()
        file = nil
      end
    }
  end,
}

function save_wav(filename, sounddata)
  -- credit to Intas and zorg on the LÖVE forum <3
  -- https://love2d.org/forums/viewtopic.php?t=86173
  local samples = {}
  for i = 0, sounddata:getSampleCount() - 1 do
    local sample = sounddata:getSample(i)
    local n
    local to16bit = sample * 32767
    if (to16bit > 0) then
      n = math.floor(math.min(to16bit, 32767))
    else
      n = math.floor(math.max(to16bit, -32768))
    end
    table.insert(samples, n)
  end
  local w = wav.create_context(filename, "w")
  w.init(sounddata:getChannelCount(), sounddata:getSampleRate(), sounddata:getBitDepth())
  w.write_samples_interlaced(samples)
  w.finish()
end