1

I am trying to make a text-to-sound-file converter with python, it reads the binary data of an input and writes a sound file that matches the data.

1 is a higher note (440 hz) and 0 is a lower note(330 hz). I have tried so many different things and my code is all over the place. Can someone please help me fix it?

import os
import numpy as np
import winsound
import wave
import struct
import math
import random
# Parameters
sampleRate = 44100    # samples per second
duration = 1           # sample duration (seconds)
frequency = 440.0       # sound frequency (Hz)
print(os.getcwd())
obj = wave.open('sound.wav','w')
obj.setnchannels(1) # mono
obj.setsampwidth(2)
obj.setframerate(sampleRate)
string=input("Enter text to turn into sound file")
binary=' '.join(format(ord(i), 'b') for i in string)
print(binary)
for i in binary:
    if i=="1":
        winsound.Beep(440, 500)
        data=struct.pack('<h', 440)
        obj.writeframesraw( data )
    elif i=="0":
        winsound.Beep(330, 500)
        data=struct.pack('<h', 330)
        obj.writeframesraw( data )
obj.close()
HSRM15
  • 11
  • 1

1 Answers1

0

You may use the 'wb' mode when you try to write (in line 13)