From 5198c50572b9e7b51f874aa638dc78ab793b3654 Mon Sep 17 00:00:00 2001 From: Florian Scheel Date: Fri, 10 Nov 2017 17:31:55 +0100 Subject: [PATCH] Obey "L" (repeats) parameter Fixes #3 --- Acoustics.moon | 46 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/Acoustics.moon b/Acoustics.moon index 6896161..a3764a1 100644 --- a/Acoustics.moon +++ b/Acoustics.moon @@ -1,8 +1,16 @@ require "lfs" MspGlobals = require "MspGlobals" -import MODULE_PATH from MspGlobals +import MODULE_PATH, OPERATING_SYSTEM_TYPE from MspGlobals +-- forward declare class, we'll need it +local Acoustics + +-- default paramter values +DEFAULT_VOLUME = 100 +DEFAULT_REPEATS = 1 + +-- search for sound files dosWildcardsToLuaPattern = (soundFile) -> suffix = soundFile\match("%.(%w+)$") if not suffix and not soundFile\find("%*$") @@ -29,10 +37,22 @@ getSoundFiles = (completeSoundPath, soundType) -> return searchFiles(MODULE_PATH .. "sounds/" .. soundPath, soundFilePattern) +-- handle sound finish events +registerAnonymousEventHandler("sysSoundFinished", (_1, _2, soundFile) -> + soundFile = soundFile\sub 2 if OPERATING_SYSTEM_TYPE == "windows" and soundFile\starts "/" + soundObject = Acoustics.playingSoundFiles[soundFile] + if soundObject + Acoustics.playingSoundFiles[soundFile] = nil + soundObject\FinishedPlaying! +) + + + +-- class declaration class Acoustics new: (completeSoundPath, properties={}) => @completeSoundPath = completeSoundPath - + for argShortName, argValue in pairs(properties) if argName = @@.ArgShortNamesToNames argShortName self[argName] = argValue @@ -40,13 +60,21 @@ class Acoustics print "MSP: server sent an unknown argument type: " .. argShortName if @volume - @volume = tonumber(@volume) or 100 + @volume = tonumber(@volume) or DEFAULT_VOLUME if @volume < 0 @volume = 0 elseif @volume > 100 @volume = 100 else - @volume = 100 + @volume = DEFAULT_VOLUME + + if @repeats + @repeats = tonumber(@repeats) or DEFAULT_REPEATS + @repeats = math.floor @repeats + if @repeats < 1 and @repeats != -1 + @repeats = 1 + else + @repeats = DEFAULT_REPEATS @url = @@defaultUrl if not @url and @@defaultUrl @@ -56,7 +84,15 @@ class Acoustics @@defaulUrl = @url if @url Play: => - playSoundFile(@files[math.random #@files], @volume) if @files and #@files > 0 + fileToPlay = @files[math.random #@files] + @@playingSoundFiles[fileToPlay] = @ + playSoundFile(fileToPlay, @volume) if @files and #@files > 0 + + FinishedPlaying: => + @repeats -= 1 if @repeats != -1 + @\Play! if @repeats == -1 or @repeats > 0 + + @playingSoundFiles = {} @ArgShortNamesToNames: (shortName) -> switch shortName