Skip to content

Instantly share code, notes, and snippets.

@juancarlospaco
Created May 16, 2016 04:56
Show Gist options
  • Save juancarlospaco/232d6858be899c9f223c27179127a8ad to your computer and use it in GitHub Desktop.
Save juancarlospaco/232d6858be899c9f223c27179127a8ad to your computer and use it in GitHub Desktop.
Crossplatform Sound Playing with StdLib only,No Sound file required. Plays an 8-Bit game style music procedurally generated on the fly.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Crossplatform Sound Playing with StdLib only,No Sound file required.
Plays an 8-Bit game style music procedurally generated on the fly."""
import os
import sys
from subprocess import call
from tempfile import gettempdir
def music8bit():
"""Crossplatform Sound Playing with StdLib only,No Sound file required."""
wavefile = os.path.join(gettempdir(), "8bit_music.wav")
music = "".join([str(
(t >> 15 & (t >> (2 if (t % 2) else 4)) %
(3 + (t >> (8 if (t % 2) else 11)) % 4) +
(t >> 10) | 42 & t >> 7 & t << 9))
for t in range(2 ** 20)
])
if not os.path.isfile(wavefile) or not os.access(wavefile, os.R_OK):
with open(wavefile, "w") as wave_file:
wave_file.write(music)
if sys.platform.startswith("linux"):
return call("aplay -c2 -r4 '{fyle}'".format(fyle=wavefile), shell=1)
if sys.platform.startswith("darwin"):
return call("afplay '{fyle}'".format(fyle=wavefile), shell=True)
if sys.platform.startswith("win"): # FIXME: This is Ugly.
return call("start /low /min '{fyle}'".format(fyle=wavefile), shell=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment