'How to play a custom sound frequency

I'm starting with python and currently working an a simple frequency "generator". I'm using pysine to play the frequencies but it can only play a frequency that is defined by a variable like this

frequency1 = 555
frequency2 = 700
frequency3 = 1000
duration = 1 # 1 second duration

In terminal, the sound options is selected by inputs

freq = input("\nOption: ")

    if freq == '1':
        print('Producing sine wave with ' + str(frequency1) + "hz and duration of " + str(duration) + " seconds.")
        pysine.sine(frequency1, duration)
        invalidcommand = False

The problem is that the custom input don't work

    elif freq == '4':
        custom = input('\nCustom frequency: ')
        print('Producing sine wave with ' + str(custom) + "hz and duration of " + str(duration) + " seconds.")
        pysine.sine(str(custom), duration)
        invalidcommand = False

The errors:

Traceback (most recent call last):
  File "\PycharmProjects\pythonProject1\venv\lib\site-packages\pysine\pysine.py", line 44, in sine
    data = np.array((np.sin(times*frequency*2*np.pi) + 1.0)*127.5, dtype=np.int8).tostring()
numpy.core._exceptions.UFuncTypeError: ufunc 'multiply' did not contain a loop with signature matching types (dtype('<U32'), dtype('<U32')) -> dtype('<U32')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/PycharmProjects/pythonProject1/doppler.py", line 51, in <module>
    doppler()
 
 File "/PycharmProjects/pythonProject1/doppler.py", line 47, in doppler
    pysine.sine(str(custom), duracao)
  
File "\PycharmProjects\pythonProject1\venv\lib\site-packages\pysine\pysine.py", line 56, in sine
    return PYSINE.sine(frequency=frequency, duration=duration)
 
 File "\PycharmProjects\pythonProject1\venv\lib\site-packages\pysine\pysine.py", line 47, in sine
    omega = 2.0*pi*frequency/self.BITRATE
NameError: name 'pi' is not defined

Is there any other way to this work?



Solution 1:[1]

pysine.sine() function takes an integer type argument, but you were passing a string which caused an error, so you can convert the input from the user to int using int():

import pysine

frequency1 = 555
frequency2 = 700
frequency3 = 1000
duration = 1

freq = input("\nOption: ")

if freq == '1':
    print('Producing sine wave with ' + str(frequency1) + "hz and duration of " + str(duration) + " seconds.")
    pysine.sine(frequency1, duration)
    invalidcommand = False

elif freq == '4':
    custom = int(input('\nCustom frequency: '))
    print('Producing sine wave with ' + str(custom) + "hz and duration of " + str(duration) + " seconds.")
    pysine.sine(custom, duration)
    invalidcommand = False

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cardstdani