Skip to content

Instantly share code, notes, and snippets.

@elderica
Last active September 13, 2025 12:31
Show Gist options
  • Save elderica/cad8b1ee115906cf8cccae2a5d3a9f3d to your computer and use it in GitHub Desktop.
Save elderica/cad8b1ee115906cf8cccae2a5d3a9f3d to your computer and use it in GitHub Desktop.
import math
import array
import io
import os
import os.path as path
import wave
import winsound
wav_hz = 44100
musictime = wav_hz * 5
frq = 440.0
def main():
wave_data = array.array('h')
for t in range(musictime):
u = 12767 * math.sin(2.0 * math.pi * frq * t / wav_hz)
wave_data.append(int(u))
with io.BytesIO() as file:
with wave.open(file, 'wb') as wav_file:
wav_file.setnchannels(1)
wav_file.setsampwidth(2)
wav_file.setframerate(wav_hz)
wav_file.writeframes(wave_data)
winsound.PlaySound(file.getbuffer(), winsound.SND_MEMORY)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment