Skip to content

Commit

Permalink
Obey "L" (repeats) parameter
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
fscheel committed Nov 10, 2017
1 parent 75852a0 commit 5198c50
Showing 1 changed file with 41 additions and 5 deletions.
46 changes: 41 additions & 5 deletions Acoustics.moon
Original file line number Diff line number Diff line change
@@ -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("%*$")
Expand All @@ -29,24 +37,44 @@ 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
else
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

Expand All @@ -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
Expand Down

0 comments on commit 5198c50

Please sign in to comment.