I've been trying the web audio api via an extern class to get access to a js API with the functions I need to load and play the sounds. That was easy... But I got the same problem as with the openfl API. Sounds would not autoplay on iOS thaks to apple restrictions. I don't know whether this aplies to flambe (BTW I didn't use it cause I'd have had to refactor my whole project)
So I'm trying to implement the Robin Burrer solution.
One of my concerns with this sort of sound spritesheet solution is that I have a lot of different dynamic content with different sound associated with them that must be loaded online. So I needed a sort of dynamic sound sprite sheet generator to dump all sounds to just one mp3 and serialize a json configuration file with the duration of each of them.
Well I created a windows patch file for this that use ffmpeg to generate the silence mp3 to separate each sound and then concatenate all the sounds together, and thought it is worth sharing here:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM Batch file to create Audio SpriteSheet
REM Author: Glantucan
REM some var definitions (Subfolder must end with a \)
SET subfolder=seasons\
SET rootPath=%CD%\
SET assetsPath=%rootPath%assets\audio\%subfolder%
SET libPath=%rootPath%lib\audio\%subfolder%
REM Silence duration as decimal fractions of a second (2 means 0.2s)
SET time=1
REM Check if subfolder exists on the lib\audio folder. Exit if not.
IF NOT EXIST %libPath% (
ECHO %libPath% folder doesn't exist. Exiting...
GOTO :eof
)
REM Check if subfolder exists on the assets\audio folder. Create it if not:
IF NOT EXIST %assetsPath% MKDIR %assetsPath%
REM Only generate if the audioSprite.mp3 doesn't exist.
IF EXIST %assetsPath%audioSprite.mp3 (
ECHO Audio SpriteSheet already generated. Exiting...
GOTO :eof
) ELSE (
goto :start
)
:start
REM Create Audio Data file
ECHO "audioData":{ > %assetsPath%audioSpriteData.json
cd %libPath%
REM Generate a silence file
ffmpeg -y -filter_complex aevalsrc=0 -t 0.%time% -ac 2 %assetsPath%0%time%s_silence.mp3
REM Measure its real duration:
REM get the duration field
FOR /f %%i IN ('ffprobe -loglevel error -show_streams %assetsPath%0%time%s_silence.mp3 ^| FINDSTR "duration=" ') DO SET duration=%%i
REM Now get its numeric value and save it to the audiodata file
FOR /f "tokens=1,2 delims== " %%a IN ("%duration%") DO SET length=%%b
ECHO "spacerLength":%length%, >> %assetsPath%audioSpriteData.json
REM write the json array for the audio sprites to the audio data file
ECHO "sprites":[ >> %assetsPath%audioSpriteData.json
REM Create file list for concatenating all files together later
SET filelist=
REM Need the number of files to set the json data file properly
for /f %%A in ('dir ^| find "File(s)"') do set totalCnt=%%A
echo
echo Number of mp3 files: %totalCnt%
echo
SET cnt=0
REM Operate over all mp3 files in Lib folder
FOR %%f IN (*) DO (
SET /a cnt+=1
REM update the file list
CALL :concat %%f
CALL :concat %assetsPath%0%time%s_silence.mp3
REM Measure mp3 file real duration:
REM get the duration field and write it down onto the data file
FOR /f %%i IN ('ffprobe -loglevel error -show_streams %%f ^| FINDSTR "duration=" ') DO (
FOR /f "tokens=1,2 delims== " %%a IN ("%%i") DO (
echo file number !cnt! of %totalCnt%
IF !cnt!==%totalCnt% (
ECHO "%%~nf":%%b >> %assetsPath%audioSpriteData.json
ECHO %%~nf: duration%%b
) ELSE (
ECHO "%%~nf":%%b, >> %assetsPath%audioSpriteData.json
ECHO %%~nf: duration%%b
)
)
)
)
ECHO ] >> %assetsPath%audioSpriteData.json
ECHO } >> %assetsPath%audioSpriteData.json
ECHO Files to concatenate: %filelist%
REM Concatenate mp3 files
TYPE %filelist% > %assetsPath%concatSprite.mp3
REM Make sure it ends up being a proper mp3 file
ffmpeg -y -i %assetsPath%concatSprite.mp3 %assetsPath%audioSprite.mp3
REM delete temporal files.
IF EXIST %assetsPath%audioSprite.mp3 DEL %assetsPath%concatSprite.mp3
IF EXIST %assetsPath%0%time%s_silence.mp3 DEL %assetsPath%0%time%s_silence.mp3
REM Go back to root folder
CD %rootPath%
GOTO :eof
:concat
REM This was necessary becaus for some reason filelist var couldn't be concatenated with itself.
REM Not an expert on Windows Batch Scripting internals and couldn't find out why.
REM UPDATE: I wasn't properly using DELAYEDEXPANSION, see how cnt is used to detect the last
REM file in the lib/assets folder not to write the last comma in the json file
SET filelist=%filelist% %1
GOTO :eof
It took me the whole day to refresh my ms-dos syntax knowledge and get this working so I hope you find it useful.