How to save all windows.beep sound in a wav file python

80 Views Asked by At

I want to save all beep sound i wav file,i came across sf.write function but dont know how to use.

import time
import winsound
import soundfile as sf
freq = 550  # Hz
dotLength = 60  # milliseconds
dashLength = dotLength * 3
pauseWords = dotLength * 7
print("hi ji")
alphaToMorse = {'a': ".-", 'b': "-...", 'c': "-.-.", 'd': "-..", 'e': ".",
                'f': "..-.", 'g': "--.", 'h': "....", 'i': "..", 'j': ".---", 'k': "-.-",
                'l': ".-..", 'm': "--", 'n': "-.", 'o': "---", 'p': ".--.", 'q': "--.-",
                'r': ".-.", 's': "...", 't': "-", 'u': "..-", 'v': "...-", 'w': ".--",
                'x': "-..-", 'y': "-.--", 'z': "--..",
                '1': ".----", '2': "..---", '3': "...--", '4': "....-", '5': ".....",
                '6': "-....", '7': "--...", '8': "---..", '9': "----.", '0': "-----",
                ' ': "/", '.': ".-.-.-", ',': "--..--", '?': "..--..", "'": ".----.",
                '@': ".--.-.", '-': "-....-", '"': ".-..-.", ':': "---...", ';': "---...",
                '=': "-...-", '!': "-.-.--", '/': "-..-.", '(': "-.--.", ')': "-.--.-",
                'á': ".--.-", 'é': "..-.."}


def morsecode():
    """
    converts text to morse code.
    prints result and calls morseaudio.
    """

    while True:
        print(''.join(input(">").strip().split()))
        message = 'why not'.join(input(">").strip().split())
        # if you enter nothing, exits method
        if message == "":
            return
        print("hu")
        # remembers characters that do not have standard morse code equivalent
        unabletoconvert = ""
        morse = ""
        for char in message.lower():
            print("yooo")
            if char in alphaToMorse:
                morse += alphaToMorse[char] + ' '
            else:
                unabletoconvert += char
        if len(unabletoconvert) != 0:
            print("These characters are unable to be converted:\n" +
                  ' '.join(unabletoconvert))
        morse = morse[:-1]
        print(morse)
        morseaudio(morse)


def beep(dur):
    """
    makes noise for specific duration.
    :param dur: duration of beep in milliseconds
    """
    winsound.Beep(freq, dur)



def pause(dur):
    """
    pauses audio for dur milliseconds
    :param dur: duration of pause in milliseconds
    """
    time.sleep(dur / 1000)


def morseaudio(morse):
    """
    plays audio conversion of morse string using inbuilt windows module.
    :param morse: morse code string.
    """
    for char in morse:
        if char == ".":
            beep(dotLength)
        elif char == "-":
            beep(dashLength)
        elif char == "/":
            pause(pauseWords)
        else:
            # char is blank space
            pause(dashLength)


morsecode()

I tried sf.write() function but dint know how to.Can you help me change the code to get the required wav file.the functions are simply creating and playing a morse which i want to save in wav file.The fuctions morseaudio(morse) converts it in morse code data.

0

There are 0 best solutions below