sfxr.lua - Generate sounds dynamically at runtime

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
nucular
Prole
Posts: 22
Joined: Sun Mar 16, 2014 1:23 pm

Re: Pure Lua port of the sfxr sound generator

Post 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"
User avatar
SiENcE
Party member
Posts: 792
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Pure Lua port of the sfxr sound generator

Post 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.
User avatar
nucular
Prole
Posts: 22
Joined: Sun Mar 16, 2014 1:23 pm

Re: Pure Lua port of the sfxr sound generator

Post 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.
User avatar
SiENcE
Party member
Posts: 792
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Pure Lua port of the sfxr sound generator

Post 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.
User avatar
nucular
Prole
Posts: 22
Joined: Sun Mar 16, 2014 1:23 pm

Re: Pure Lua port of the sfxr sound generator

Post 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.
User avatar
murks
Party member
Posts: 185
Joined: Tue Jun 03, 2014 4:18 pm

Re: Pure Lua port of the sfxr sound generator

Post 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?
User avatar
SiENcE
Party member
Posts: 792
Joined: Thu Jul 24, 2008 2:25 pm
Location: Berlin/Germany
Contact:

Re: Pure Lua port of the sfxr sound generator

Post 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.
User avatar
nucular
Prole
Posts: 22
Joined: Sun Mar 16, 2014 1:23 pm

Re: Pure Lua port of the sfxr sound generator

Post 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.
User avatar
soulaymenc
Prole
Posts: 36
Joined: Thu Jul 11, 2013 2:03 pm
Contact:

Re: Pure Lua port of the sfxr sound generator

Post by soulaymenc »

Love it! Thank you very much.
This world is so strange.
User avatar
undef
Party member
Posts: 438
Joined: Mon Jun 10, 2013 3:09 pm
Location: Berlin
Contact:

Re: Pure Lua port of the sfxr sound generator

Post by undef »

Ah too bad it's still quite buggy, but it's a wonderful program regardless!
twitter | steam | indieDB

Check out quadrant on Steam!
Post Reply

Who is online

Users browsing this forum: No registered users and 225 guests