Page 2 of 4

Re: Pure Lua port of the sfxr sound generator

Posted: Thu Jun 05, 2014 8:54 pm
by nucular
Bumping a bit, also I added functions for generating a simple Lua file from the sound parameters and for loading it. You can use it like this if you want to work with the Löve file API instead of the basic Lua one:

Code: Select all

local sound = sfxr.newSound()
sound:randomize()
local f = love.filesystem.newFile("sound.lua")
f:open("w")
sound:save(f)
f:flush()
f:close()

Code: Select all

local sound = sfxr.newSound()
local f = love.filesystem.newFile("sound.lua")
f:open("r")
sound:load(f)
f:close()
sound:play()
If you pass a second argument to sound:save(), it will generate a somewhat smaller file because no indention and linebreaks are added.
Here are the results of these examples, compressed and uncompressed:

Code: Select all

local 
s={
 change={
  amount=0.39114666484126;
  speed=0.53098955685005;
 };
 vibrato={
  delay=-0.94992868273876;
  depth=-0.024465061542777;
  speed=0.84200208799958;
 };
 frequency={
  slide=-0.027968131846831;
  limit=0;
  start=0.23970054770736;
  dslide=0.59757475861064;
 };
 phaser={
  sweep=0.4304590939705;
  offset=-0.070899490398386;
 };
 duty={
  ratio=-0.3660692363753;
  sweep=-0.5988394794907;
 };
 repeatspeed=0.62650828993079;
 highpass={
  sweep=-0.19473133535069;
  cutoff=0.6262447349085;
 };
 envelope={
  attack=0.044302194827103;
  punch=0.011362092672399;
  decay=-0.55449086076159;
  sustain=0.47922909089777;
 };
 volume={
 };
 lowpass={
  resonance=-0.0013938250035848;
  sweep=2.243246748532e-006;
  cutoff=0.67252042718807;
 };
};
return s, "0.0"

Code: Select all

local s={change={amount=0.39114666484126;speed=0.53098955685005;};vibrato={delay=-0.94992868273876;depth=-0.024465061542777;speed=0.84200208799958;};frequency={slide=-0.027968131846831;limit=0;start=0.23970054770736;dslide=0.59757475861064;};phaser={sweep=0.4304590939705;offset=-0.070899490398386;};duty={ratio=-0.3660692363753;sweep=-0.5988394794907;};repeatspeed=0.62650828993079;highpass={sweep=-0.19473133535069;cutoff=0.6262447349085;};envelope={attack=0.044302194827103;punch=0.011362092672399;decay=-0.55449086076159;sustain=0.47922909089777;};volume={};lowpass={resonance=-0.0013938250035848;sweep=2.243246748532e-006;cutoff=0.67252042718807;};};
return s, "0.0"

Re: Pure Lua port of the sfxr sound generator

Posted: Fri Jun 06, 2014 10:42 am
by SiENcE
So I can use this tool to create sound effects and than use the included library in my game to playback this effects instead of using exported wavs? This would save some diskspace and allowes to modify the soundparameters during playback.

Re: Pure Lua port of the sfxr sound generator

Posted: Fri Jun 06, 2014 12:46 pm
by nucular
Yeah, that's the intended use of it. You could also generate all sound effects at the first run of your game, export them as WAV files to the data directory and load them like ordinary sounds in the next runs. This is especially useful when you want to use many sounds with only a few changes.

Re: Pure Lua port of the sfxr sound generator

Posted: Fri Jun 06, 2014 2:23 pm
by SiENcE
Sure, but for me the real benefit of this is to not use pregenerated sounds (waves). I can't see how I can save and load the sound parameters to a file from your tool.

Re: Pure Lua port of the sfxr sound generator

Posted: Fri Jun 06, 2014 4:25 pm
by nucular
You simply create a new Sound object, fill in the parameters and then call :save(f) on it. You can pass either a path for io.open, a Lua file handle or an open love.filesystem.File instance as f.

Code: Select all

sfxr = require("sfxr")
function love.load()
    sound = sfxr.newSound()
end

function love.keypressed(k)
    if k == " " then
        sound:randomize()
        sound:play()
    elseif k == "return" then
        local f = love.filesystem.newFile("out.lua")
        f:open("w")
        sound:save(f)
    end
end
This will randomize the sound on each press of the spacebar and save it to "out.lua" when you press return. The output can be found inside the game's save directory, e.g "C:\Users\[user name]\AppData\Roaming\LOVE\sfxrlua" on Windows. Loading would just be doing

Code: Select all

sfxr = require("sfxr")
function love.load()
    sound = sfxr.newSound("out.lua")
    -- or
    -- sound = sfxr.newSound()
    -- sound:load("out.lua")
end
If there's no out.lua inside the save directory, it will look at the games .love archive or main folder instead.

Re: Pure Lua port of the sfxr sound generator

Posted: Fri Jun 06, 2014 9:11 pm
by murks
Thanks, this is very nice.
I just played with the original sfxr for hours before seeing this.
Overall the playback volume produced by your version seems a lot higher, but I didn't measure it.
The GUI looks nice than sfxr but has one drawback: you can no longer see all the options/sliders at a glance.

The possibility to use it by code is very nice. It would be great for slight variation of sounds. Is it possible to mutate predefined sounds within a predefined range?

Re: Pure Lua port of the sfxr sound generator

Posted: Sat Jun 07, 2014 10:19 am
by SiENcE
@nucular: Hm this is not really what i meant. I want to adjust all the parameters in your sfxr tool and than save it too a file. Than i want to load this saved parameter file and play it in my game. The suggestion of murks is great too. After each playback you can call 'mutate' and the effect varies between each play. This brings in more variety than to playback waves.

Randomize is no option, because you don't want to playback random sounds in a game.

Re: Pure Lua port of the sfxr sound generator

Posted: Sat Jun 07, 2014 11:39 am
by nucular
@murks
Thanks for the feedback. I intend on making a resizable GUI when I get around to finish my implementation of anchors for LöveFrames.
You can pass a number between 0 and 1 to Sound:mutate. The second argument is the random seed and the third will enable mutation of the frequency parameters if given.

@SiENcE
Oh, you werr talking about having a "Save" and "Load" button in the demo GUI. Yeah, I want to create a kind of simple filepicker for LöveFrames before that though, so that it'd be more comfortable to use.

Re: Pure Lua port of the sfxr sound generator

Posted: Sun Jul 20, 2014 12:32 pm
by soulaymenc
Love it! Thank you very much.

Re: Pure Lua port of the sfxr sound generator

Posted: Sun Jul 20, 2014 12:49 pm
by undef
Ah too bad it's still quite buggy, but it's a wonderful program regardless!