From 3cf88e042ea4510b2f318299d43140602a23c436 Mon Sep 17 00:00:00 2001 From: nucular Date: Wed, 7 May 2014 16:58:37 +0200 Subject: [PATCH] Simplify module structure --- README.md | 4 ++-- sfxr.lua | 28 ++++++++++++---------------- 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 84cbf89..dd6666e 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ With Löve2D 0.9: local sfxr = require("sfxr") function love.load() - local sound = sfxr.new() + local sound = sfxr.newSound() sound:randomize() love.audio.newSource(sound:generateSoundData()):play() end @@ -29,7 +29,7 @@ local sfxr = require("sfxr") local driverId = ao.defaultDriverId() local device = ao.openLive(driverId, {bits = 16, rate = 44100, channels = 1}) -local sound = sfxr.new() +local sound = sfxr.newSound() sound:randomize() local buffer = sound:generateString() diff --git a/sfxr.lua b/sfxr.lua index 5901aac..22caf04 100644 --- a/sfxr.lua +++ b/sfxr.lua @@ -22,13 +22,6 @@ SOFTWARE. ]]-- local sfxr = {} -sfxr.__index = sfxr - -local function new() - local obj = setmetatable({}, sfxr) - obj:__init() - return obj -end -- Constants @@ -63,9 +56,12 @@ local function cpypol(a, b) end end --- Class functions +-- The main Sound class -function sfxr:__init() +sfxr.Sound = {} +sfxr.Sound.__index = sfxr.Sound + +function sfxr.Sound:__init() -- Build tables to store the parameters in self.volume = {} self.envelope = {} @@ -193,12 +189,12 @@ function sfxr:generate() end end +-- Constructor -sfxr:__init() +function sfxr.newSound(...) + local instance = setmetatable({}, sfxr.Sound) + instance:__init(...) + return instance +end -return setmetatable({new = new}, - { - __call = function(_, ...) - return new(...) - end - }) +return sfxr \ No newline at end of file