#!/usr/bin/env python
################################################################################
# soundboard.py - A script to generate an HTML5 document that can act as a
# soundboard for audio clips. Currently the script only generates one file and
# has no stylesheet. For this to work use the following directory struture:
#
# .
# +-- _audio_clips1
# | +-- audio_file1.wav
# | +-- audio_file2.wav
# +-- _audio_clips2
# | +-- audio_file1.wav
# +-- soundboard.py
#
# To run the script use the following: python soundboard.py > soundboard.html
################################################################################
import os
BASEDIR = os.path.dirname(os.path.abspath(__file__))
HTML_AUDIO_BUTTON = """
<audio preload="auto" id="{id}">
<source src="{dir_name}/{file_name}" type="audio/{type}">
</audio><button onclick="playAudio('{id}')">{label}</button>
"""
HTML_BODY = """
<!DOCTYPE html>
<html>
<head><title>Sound Board</title></head>
<body>
{0}
<script>
function playAudio(id) {{
var audioBit = document.getElementById(id);
audioBit.play();
}}
</script>
</body>
</html>
"""
def get_audio_dir_list():
audio_dir_list = []
with os.scandir(BASEDIR) as home_dir:
for item in home_dir:
if item.is_dir():
audio_dir_list.append(item.name)
return audio_dir_list
def get_audio_files(path):
audio_file_list = []
with os.scandir(path) as audio_dir:
for item in audio_dir:
if item.is_file():
audio_file_list.append(item.name)
return audio_file_list
def make_button(dir_name, file_name):
js_id, audio_type = file_name.split('.')
label = js_id.replace('_', ' ')
return HTML_AUDIO_BUTTON.format(id=js_id, dir_name=dir_name, file_name=file_name,
type=audio_type, label=label)
def make_soundboard(dir_name, button_list):
header = dir_name.replace('_', ' ').upper()
buttons = ' '.join(button_list)
return f'<h1>{header}</h1><div>{buttons}</div>'
def main():
button_list_by_dir = {}
for dir_name in get_audio_dir_list():
dir_path = f'{BASEDIR}{os.sep}{dir_name}{os.sep}'
button_list_by_dir[dir_name] = [make_button(dir_name, fn) for fn in get_audio_files(dir_path)]
soundboard_list = [make_soundboard(dn, bl) for dn, bl in button_list_by_dir.items()]
#print(''.join(soundboard_list))
print(HTML_BODY.format(str(''.join(soundboard_list))))
if __name__ == "__main__":
main()
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎