Advertisement
andrewb

soundboard.py

May 17th, 2020
1,748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. #!/usr/bin/env python
  2. ################################################################################
  3. # soundboard.py - A script to generate an HTML5 document that can act as a
  4. # soundboard for audio clips. Currently the script only generates one file and
  5. # has no stylesheet. For this to work use the following directory struture:
  6. #
  7. # .
  8. # +-- _audio_clips1
  9. # |   +-- audio_file1.wav
  10. # |   +-- audio_file2.wav
  11. # +-- _audio_clips2
  12. # |   +-- audio_file1.wav
  13. # +-- soundboard.py
  14. #
  15. # To run the script use the following: python soundboard.py > soundboard.html
  16. ################################################################################
  17. import os
  18.  
  19. BASEDIR = os.path.dirname(os.path.abspath(__file__))
  20.  
  21. HTML_AUDIO_BUTTON = """
  22. <audio preload="auto" id="{id}">
  23. <source src="{dir_name}/{file_name}" type="audio/{type}">
  24. </audio><button onclick="playAudio('{id}')">{label}</button>
  25. """
  26.  
  27. HTML_BODY = """
  28. <!DOCTYPE html>
  29. <html>
  30. <head><title>Sound Board</title></head>
  31. <body>
  32. {0}
  33. <script>
  34. function playAudio(id) {{
  35. var audioBit = document.getElementById(id);
  36. audioBit.play();
  37. }}
  38. </script>
  39. </body>
  40. </html>
  41. """
  42.  
  43.  
  44. def get_audio_dir_list():
  45.     audio_dir_list = []
  46.     with os.scandir(BASEDIR) as home_dir:
  47.         for item in home_dir:
  48.             if item.is_dir():
  49.                 audio_dir_list.append(item.name)
  50.     return audio_dir_list
  51.  
  52.  
  53. def get_audio_files(path):
  54.     audio_file_list = []
  55.     with os.scandir(path) as audio_dir:
  56.         for item in audio_dir:
  57.             if item.is_file():
  58.                 audio_file_list.append(item.name)
  59.     return audio_file_list
  60.  
  61.  
  62. def make_button(dir_name, file_name):
  63.     js_id, audio_type = file_name.split('.')
  64.     label = js_id.replace('_', ' ')
  65.     return HTML_AUDIO_BUTTON.format(id=js_id, dir_name=dir_name, file_name=file_name,
  66.                                     type=audio_type, label=label)
  67.  
  68.  
  69. def make_soundboard(dir_name, button_list):
  70.     header = dir_name.replace('_', ' ').upper()
  71.     buttons = ' '.join(button_list)
  72.     return f'<h1>{header}</h1><div>{buttons}</div>'
  73.  
  74.  
  75. def main():
  76.     button_list_by_dir = {}
  77.     for dir_name in get_audio_dir_list():
  78.         dir_path = f'{BASEDIR}{os.sep}{dir_name}{os.sep}'
  79.         button_list_by_dir[dir_name] = [make_button(dir_name, fn) for fn in get_audio_files(dir_path)]
  80.     soundboard_list = [make_soundboard(dn, bl) for dn, bl in button_list_by_dir.items()]
  81.     #print(''.join(soundboard_list))
  82.     print(HTML_BODY.format(str(''.join(soundboard_list))))
  83.  
  84.  
  85. if __name__ == "__main__":
  86.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement