Skip to content

Instantly share code, notes, and snippets.

@sroccaserra
Last active December 23, 2021 01:45
Show Gist options
  • Save sroccaserra/b704030e9978ca5a0e3a6da837f03806 to your computer and use it in GitHub Desktop.
Save sroccaserra/b704030e9978ca5a0e3a6da837f03806 to your computer and use it in GitHub Desktop.
Generate a single cycle pulse wave file in Python 3
import wave
fichier = 'pulse.wav'
def main(fichier):
"""
Generate a single cycle mono pulse of 600 unsigned 8 bytes samples at
44100 Hz. This should be a D. Loop it and add 10 steps in your sampler
to tune it to C.
- https://docs.python.org/3/library/wave.html
"""
with wave.open(fichier, 'wb') as wave_buffer:
nchannels = 1
sampwidth = 1
framerate = 44100
nframes = 600
comptype = 'NONE'
compname = 'not compressed'
wave_buffer.setparams((nchannels, sampwidth, framerate, nframes, comptype, compname))
sound_ints = [255 if i < 300 else 0 for i in range(nframes)]
sound_bytes = bytearray(sound_ints)
wave_buffer.writeframesraw(sound_bytes)
if __name__ == '__main__':
main(fichier)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment