0

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?

seven
  • 11
  • 1
  • take a look at https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.write.html –  Apr 27 '22 at 10:13
  • i did take a look at that, but i dont know how to use "scipy.io.wavfile.write" function. isnt it possible to just "stack" the audio files and get a big file at the end? doesnt even need to be a wav file – seven Apr 27 '22 at 11:19
  • in the bottom of the link you have an example of how to generate a 100Hz sine wav. you can use this example to generate a short sine wave for a dot and a long one for a dash and you will have your morse code. –  Apr 27 '22 at 11:36

1 Answers1

1

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)

seven
  • 11
  • 1