Search code examples
python-3.xaudiofrequencybeep

How can i make sound with frequency in python3?


I tried this, but it didn't made more than an empty line :

import os
a=300
b=2000
os.system('play --no-show-progress --null --channels 1 synth %s sine %f' % ( a, b))

Solution

  • Easy ways to play a beep sound of given frequency and duration in Python:

    frequency = 1000 # Hertz
    duration  = 2000 # milliseconds
    

    On Windows:

    import winsound
    winsound.Beep(frequency, duration)
    

    On Linux:

    # SoX must be installed using 'sudo apt-get install sox' in the terminal
    import os
    os.system('play -n synth %s sin %s' % (duration/1000, frequency))
    

    On macOS:

    # First install Homebrew (https://brew.sh/) 
    # and then SoX using 'brew install sox' in the terminal
    import os
    os.system('play -n synth %s sin %s' % (duration/1000, frequency))
    

    Cross-platform:

    Using PyAudio module and a bit of coding: https://stackoverflow.com/a/27978895