How do i edit/create a wave file for morse code in python?

455 views Asked by At

im trying to make a python morse code translater, which takes in a string and should return a wav file containing morse code.

now my problem is, how do i edit or create a wav file from text?

i wanted to do something like this (not real code):

def morse_to_wav(morse):
    for char in morse:
        if char == '.':
            wavfile add "short.wav" ## just adds the sound to an existing wav file
        if char == '-':
            wavfile add "long.wav"  ## same here
        if char == ' ':
            wavfile add "pause.wav" ## same here

do you have any idea what i could do? or any other solution?

1

There are 1 answers

0
seven On

Soooo, I found a solution to my problem: I mixed everything i found and this is what I got: thanks to @Sembei Norimaki @Mathhew Walker (idk how to tag people) I used code from this post (thats from Mathhew Walker) Basically, i read my audio files, put them in a list, concatenate that and then use write to make a wav file. Its not very pretty but it works

from scipy.io.wavfile import write
import wave, numpy

def get_npaudiodata(audiofile):
    # Read file to get buffer                                                                                               
    ifile = wave.open(audiofile)
    samples = ifile.getnframes()
    audio = ifile.readframes(samples)

    # Convert buffer to float32 using NumPy                                                                                 
    audio_as_np_int16 = numpy.frombuffer(audio, dtype=numpy.int16)
    audio_as_np_float32 = audio_as_np_int16.astype(numpy.float32)

    # Normalise float32 array so that values are between -1.0 and +1.0                                                      
    max_int16 = 2**15
    audio_normalised = audio_as_np_float32 / max_int16
    return audio_normalised

audio_short = get_npaudiodata("short.wav")
audio_long = get_npaudiodata("long.wav")
audio_pause = get_npaudiodata("pause.wav")
nlist = [audio_short,audio_short,audio_short,audio_long,audio_long,audio_long,audio_short,audio_short,audio_short] #Thats SOS in morse

nparray = numpy.concatenate(nlist, axis=None)
write("example.wav", 10000, nparray)