Skip to content

Instantly share code, notes, and snippets.

@mds2
Created June 30, 2020 00:49
Show Gist options
  • Save mds2/f05a6ae50103533eba139b954112bc7a to your computer and use it in GitHub Desktop.
Save mds2/f05a6ae50103533eba139b954112bc7a to your computer and use it in GitHub Desktop.
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wave
>>> writer = wave.open("foo.wav", "wb")
>>> writer.setnchannels(2)
>>> from math import sin, cos
>>> writer.setsampwidth(1)
>>> writer.setframerate(44100)
>>> vals_left = [sin(3.0 * sin(3.14159265 * 440 * i / 44100)) for i in range(44100)]
>>> vals_right = [sin(3.0 * sin(2.0 + 3.14159265 * 440 * i / 44100)) for i in range(44100)]
>>> interleaved = [val for pair in zip(vals_left, vals_right) for val in pair]
>>> len(interleaved)
88200
>>> int_vals = [int(255 * (0.5 + 0.5*x)) for x in interleaved]
>>> byte_vals = bytearray(int_vals)
>>> writer.setnframes(44100)
>>> writer.writeframes(byte_vals)
>>> writer.close()
>>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment