-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
play_sound.fluid
65 lines (47 loc) · 2.01 KB
/
play_sound.fluid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
-- This script will play WAV and MP3 files for the user.
----------------------------------------------------------------------------------------------------------------------
-- This function identifies the type of file being loaded and uses the correct class to open it.
function id_file(Path)
local err, class = mSys.IdentifyFile(Path, 'Open', IDF_IGNORE_HOST)
if (class == mSys.StrHash('sound')) then
return obj.new('sound', { path=Path })
elseif (class == mSys.StrHash('music')) then
return obj.new('music', { path=Path })
end
end
----------------------------------------------------------------------------------------------------------------------
-- Retrieve the sound sample path from the command line arguments
local src = arg('src')
if (src == nil) then
print('Please supply a source audio file with "src=[file]"')
return
end
local seek = arg('seek')
local player = id_file(src)
if not player then
print('Failed to identify the type of the source file "' .. src .. '"')
return
end
-- Use the processing API to help us manage the program flow
local proc = processing.new()
-- Subscribe to OnStop so that we can exit once the sample has fully played.
player.onStop = function(Sound)
proc.signal()
end
-- User can request seeking to a position expressed as 0.0 - 1.0
if (seek) then
seek = tonumber(seek)
if (seek >= 0) and (seek <= 1.0) then
player.acSeek(seek, SEEK_RELATIVE)
else
print('Seek must be expressed as a value from 0.0 to 1.0')
end
end
-- Play the sound and wait for it to finish
if (player.acActivate() == ERR_Okay) then
print('Press CTRL-C to exit or wait for sound sample to finish. Duration: ' .. string.format('%.2fs', player.duration))
proc.sleep() -- This will wake on receipt of a signal.
proc.sleep(player.audio.mixerLag) -- Slight pause to flush any buffered audio before exiting
else
print('Failed to play audio file.')
end