Skip to content

Instantly share code, notes, and snippets.

@Simon-L
Created October 11, 2023 13:55
Show Gist options
  • Save Simon-L/af09988043775b90c2fc1dba6b937c33 to your computer and use it in GitHub Desktop.
Save Simon-L/af09988043775b90c2fc1dba6b937c33 to your computer and use it in GitHub Desktop.
Example usage of revx2.lua to generate Faust file containing the loop samples directly
-- wget https://github.com/rxi/json.lua/raw/master/json.lua
local revx2 = require("revx2")
local inspect = require 'inspect'
local json = require "json"
local wav = require'luawav'
local path = "/path/to/json.json"
local json_file = io.open(path)
local loop = json.decode(json_file:read("*all"))
print(inspect(loop))
local base_faust = [[
import("stdfaust.lib");
]]
function addEnvironnement(str, name, content)
local env = name .. " = environment {\n" .. content .. "};\n"
return str .. env
end
function addVariable(str, name, value)
local var = name .. " = " .. value .. ";\n"
return str .. var
end
function addProcess(str, content)
local proc = "\nprocess = " .. content .. ";\n"
return str .. proc
end
local loop_vars = ""
loop_vars = addVariable(loop_vars, " slices", loop.effective_slices)
loop_vars = addVariable(loop_vars, " tempo", loop.tempo)
local start_samples = "waveform{ "
local length_samples = "waveform{ "
for slice, tbl in ipairs(loop.slices_list) do
start_samples = start_samples .. tbl.start .. ", "
length_samples = length_samples .. tbl.length .. ", "
print(slice, tbl.start, tbl.length, " -> ", tbl.start + tbl.length)
end
start_samples = string.sub(start_samples, 1, -3) .. " }"
length_samples = string.sub(length_samples, 1, -3) .. " }"
loop_vars = addVariable(loop_vars, " start", start_samples)
loop_vars = addVariable(loop_vars, " length", length_samples)
local meta_and_samples = wav.drwav_open_and_read_pcm_frames_f32(loop.wav_file)
print(inspect(meta_and_samples, {depth=1}))
print(#meta_and_samples.samples / meta_and_samples.channels, (#meta_and_samples.samples / meta_and_samples.channels) / meta_and_samples.sampleRate)
local loop_samples = "waveform{ " .. table.concat(meta_and_samples.samples, ", ") .. "}"
loop_vars = addVariable(loop_vars, " loop_samples", loop_samples)
local loop_file = base_faust
loop_file = addEnvironnement(loop_file, "loop_1", loop_vars)
-- loop_file = addProcess(loop_file, "0.0")
local lib_path = string.sub(path, 1, -5) .. "lib"
local lib_file = io.open(lib_path, 'w')
lib_file:write(loop_file)
lib_file:close()
print("Saved to " .. lib_path)
-- os.execute("mg " .. lib_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment