Last active
August 29, 2015 14:00
-
-
Save WetDesertRock/11297717 to your computer and use it in GitHub Desktop.
Instrumentation - A .lua file that will help you create software instruments.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- instrumentation | |
-- | |
-- The MIT License (MIT) | |
-- | |
-- Copyright (c) 2014, DesertRock | |
-- | |
-- 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. | |
-- | |
-- Notice that this license does not apply to the dependancy on the music.lua and | |
-- classic.lua. There is also a hidden depenancy on slam.lua. This project should | |
-- be included by the main application. | |
-- These libraries can be found here: | |
-- classic.lua - https://github.com/rxi/classic | |
-- music.lua - https://github.com/WetDesertRock/music.lua | |
-- slam.lua - https://github.com/vrld/slam | |
-- | |
-- To use, require this file. | |
-- Example of usage: | |
-- | |
-- -- Require the slam and the file | |
-- require("libraries/slam") | |
-- Instrument = require('instrument) | |
-- | |
-- -- Create the instrument | |
-- ins = Instrument.Instrument() | |
-- | |
-- -- Load samples from folder. Samples must be labled by their midi note name in | |
-- -- the folder. Example folder structure: | |
-- -- sounds/ | |
-- -- mysynth/ | |
-- -- 60.wav | |
-- -- 48.wav | |
-- -- 72.wav | |
-- -- This command will load those sounds. The final slash is necessary. | |
-- ins:readFromFolder("sounds/mysynth/") | |
-- | |
-- -- You can also load custom samples by using the method :setSource(note,src) | |
-- -- Note is the midi note number that corrosponds to the sound source src. | |
-- ins:setSource(60, love.audio.newSource("sounds/mysynth/60.wav", 'static') | |
-- | |
-- -- Play sounds: | |
-- note = ins:play(24) | |
-- note:setVolume(0.5) | |
-- | |
local Object = require(... .. ".classic") | |
local mus = require(... .. ".music") | |
local tracks = {} | |
local function tablelength(t) | |
local c = 0 | |
for _,_ in pairs(t) do c = c+1 end | |
return c | |
end | |
tracks.Instrument = Object:extend() | |
function tracks.Instrument:new(soundfolder,temperment) | |
if temperment == nil then | |
self.temperment = "equal" | |
else | |
self.temperment = temperment | |
end | |
self.sources = {} | |
end | |
function tracks.Instrument:setSource(note,src) | |
self.sources[note] = src | |
end | |
function tracks.Instrument:readFromFolder(soundfolder) | |
local files = love.filesystem.getDirectoryItems(soundfolder) | |
for _,f in pairs(files) do | |
local note = tonumber(f:match("(%d*)")) | |
if note ~= nil then | |
local src = love.audio.newSource(soundfolder..f, 'static') | |
self:setSource(note,src) | |
end | |
end | |
end | |
function tracks.Instrument:play(note) | |
assert(tablelength(self.sources)~=0,"Instrument does not have any sources!") | |
local mindist = 255 -- There cannot be anything greater than this, right? | |
local minkey = nil | |
for n,snd in pairs(self.sources) do | |
local d = math.abs(note-n) | |
if d < mindist then | |
mindist = d | |
minkey = n | |
end | |
end | |
local pr = mus.pitchRatio(note-minkey,self.temperment) | |
local snd = self.sources[minkey]:play() | |
snd:setPitch(pr) | |
return snd | |
end | |
return tracks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment