Last active
December 10, 2020 00:40
-
-
Save jeffholst/205273ead65cb72681fdb0ccc36f8ba8 to your computer and use it in GitHub Desktop.
Sound Player module for Attract Mode
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
fe.load_module("file"); | |
DEBUG_SOUNDPLAYER <- true; | |
/* | |
Author: Jeff Holst | |
This Attract Mode module randomly plays sound files found in a supplied path. | |
If a textObj (fe.add_text object) is passed in, it's msg property will be | |
set with the sound file name. This is useful to display the sound file name | |
in a layout. | |
Usage: | |
- place this file in attract/modules directory | |
- place mp3 files in appropriate path | |
- add code below to layout.nut | |
fe.load_module("sound-player"); | |
local title = fe.add_text("", fly*0.11, fly*0.917, flw*0.7, flh*0.1); | |
title.align = Align.Left; | |
title.charsize = flh*0.028; | |
title.set_rgb(255, 120, 0); | |
local sp = SoundPlayer("/home/pi/RetroPie/roms/music/", title); | |
*/ | |
class SoundPlayer | |
{ | |
m_index = -1; // index of playing sound file | |
m_sounds = []; // array of preloaded sound files | |
m_textObj = null; // optional fe.add_text from layout | |
constructor ( path = "/home/pi/RetroPie/roms/music/", textObj = null ) | |
{ | |
if ( DEBUG_SOUNDPLAYER ) print( "SOUNDPLAYER: Starting SoundPlayer\n" ); | |
m_textObj = textObj; | |
m_sounds = loadFiles(path); // load sound files | |
// callback will start new random mp3 once current one stops playing | |
fe.add_ticks_callback( this, "onTick" ); | |
}; | |
function onTick ( ttime ) | |
{ | |
if( m_index == -1 || !m_sounds[m_index].playing ) | |
{ | |
playNextSound(); | |
} | |
} | |
function playNextSound() | |
{ | |
m_index = m_index + 1; | |
if (m_index >= m_sounds.len()) | |
{ | |
m_index = 0; // start playing songs from beginning | |
} | |
m_sounds[m_index].playing = true; | |
if ( DEBUG_SOUNDPLAYER ) print( "SOUNDPLAYER: Playing '" + m_sounds[m_index].file_name + "'\n" ); | |
if (m_textObj != null) | |
{ | |
m_textObj.msg = getFileName(); | |
} | |
} | |
function loadFiles(path) | |
{ | |
local loop; // used to loop through files in 'path' | |
local files; // list of all files and dirs from 'path' | |
local fileName; // fully qualified file name | |
local soundArray; // mp3 files | |
local randomizedArray; // mp3 files, but in random order | |
files = DirectoryListing(path, true); | |
if ( DEBUG_SOUNDPLAYER ) print( "SOUNDPLAYER: Found (" + files.results.len() + ") files in '" + path + "'\n" ); | |
soundArray = []; | |
for (loop=0; loop < files.results.len(); loop++) | |
{ | |
fileName = files.results[loop]; | |
if ( hasSoundExt(fileName)) | |
{ | |
soundArray.push(fe.add_sound(fileName)); | |
} | |
else | |
{ | |
if ( DEBUG_SOUNDPLAYER ) print( "SOUNDPLAYER: Omitting '" + fileName + "'\n" ); | |
} | |
} | |
if ( DEBUG_SOUNDPLAYER ) print( "SOUNDPLAYER: Loaded (" + soundArray.len() + ") total sound files\n" ); | |
randomizedArray = []; | |
while (soundArray.len() > 0) | |
{ | |
local idx = rand() % soundArray.len(); | |
randomizedArray.append(soundArray[idx]); | |
soundArray.remove(idx); | |
} | |
return randomizedArray; | |
} | |
function hasSoundExt (fileName) | |
{ | |
local rval; // return value | |
local fileExt; // file extension | |
local fileLen; // file length in characters | |
rval = false; | |
fileLen = fileName.len(); | |
// make sure file name has at least 5 characters (i.e. 'x.mp3') | |
if (fileLen > 5) | |
{ | |
// get last 3 characters of filename | |
fileExt = fileName.slice( fileLen - 3); | |
if (fileExt == "mp3" || fileExt == "MP3") | |
{ | |
rval = true; | |
} | |
} | |
return rval; | |
} | |
function getFileName() | |
{ | |
local fullPath = m_sounds[m_index].file_name; // the file name with full path | |
local splitAry = split(fullPath, "/"); // split all /'s in path | |
local file = splitAry[splitAry.len() -1]; // last item after last / | |
local fileNoExt = file.slice(0, file.len() - 4); // strip last 4 chars (i.e. .mp3) | |
return fileNoExt; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment