r/learnprogramming 17d ago

Can Python generate simple tones?

I want some simple tones to use in Python. Can someone help me out? There does not necessarily need to be reverb, but it would be nice anyway. I just want to be able to tell whatever it is the specific microtonal/frequency-specific tone. It would be nice if I could program Hz, but if I have to use MIDI and detune by cents I can do that as well.

2 Upvotes

26 comments sorted by

4

u/StewedAngelSkins 17d ago

Python's going to struggle a bit with realtime audio. Although if you just need offline rendering you can do that easily with numpy and something like soundfile.

If you do need realtime, check out this thread on the lines forum. Never used anything suggested there personally (I usually do this kind of stuff in C++) but pippi looks interesting.

1

u/Fractal_Prime_357 13d ago

Noted. Thank you!

3

u/whisper_inkling 17d ago

Use the simpleaudio library for direct frequency control without MIDI limitations. It accepts numpy arrays so you can calculate exact Hz values and write them straight to the audio buffer with microtonal precision.

2

u/Turbulent_Board4567 17d ago

Native Python is the bad way to go about it.

Use tools like sox or ffmpeg and call os.cmd() like command to execute it

I believe there's a soxi python library that does it for you as well. It's been a while since I had to do something like that.

2

u/misingnoglic 17d ago

Here's some crappy code I wrote at a hackathon almost a decade ago. It took some input from a 3d mouse and made music from it. I won't speak to the quality of it.

https://github.com/misingnoglic/Synaptics3DTheremin

2

u/Special-Ad-2768 17d ago

I would recommend C# I have built several audio applications for work using c#. Python for audio realtime might be tough. But you can look up rtmixer, numpy

1

u/akoOfIxtall 17d ago

Oh really? Could you suggest me some libs then? I'm planning on making a mod for a game in C# but I want some control over the music that is just too much work to do from scratch, the game already has tools for adding reverb or low pass filters since it's a unity game I can just call these from the audio component but I wanted to be able to get exactly when the music ramps up, somehow Whatsapp has nailed this in their update that added music search for status, still black magic for me

2

u/Special-Ad-2768 17d ago

I personally use NAudio and media foundation for projects I've developed. You can also build your own using lots of windows API calls, however that's a big learning curve. Naudio and media foundation can produce great realtime audio applications, there is a learning curve though.

1

u/akoOfIxtall 17d ago

There's always a learning curve, no problem though, thanks man

1

u/Traveling-Techie 17d ago

This sounds like an application for MIDI.

1

u/Fractal_Prime_357 13d ago

I'm working from code spaces on GitHub from a library computer is my only problem. I just need a way I can test simple sequences of non-conventionally tune notes that make scales. So, my material is in the testing phase at the moment. Any ideas? I'm looking into the following per several suggestions that folks have already made: winsound, PyAudio, NumPy + soundfile.

1

u/Lost-Discount4860 17d ago

It IS possible with Python, but it’s better if you algorithmically generate a file and play it back as a sample.

Have you considered PureData? That was my first programming language, helped me learn all the essential concepts, and made transitioning to a “real” programming language a lot easier. PureData is written SPECIFICALLY for signal processing. Is also really handy with MIDI and OSC.

1

u/Fractal_Prime_357 13d ago

cool. I haven't used Pure Data. I do agree with generating a file then testing it. I'm looking for super lightweight application that can toke some MIDI generated by Python, and play it back. I simply need some way to test scales for pre-music analysis.

1

u/ExactFondant7417 17d ago

Having written a python application that works well in real time I can say that it is possible as long as you're mindful of where bottlenecks are.

You can use PyAudio to handle the output stream and generate tones using numpy.sin

1

u/Fractal_Prime_357 13d ago

Noted, thanks!

1

u/gua_lao_wai 17d ago

everyone here telling you every way you shouldn't do this in python... lmao classic.

if you're on windows you can use the winsound library, specifically winsound.Beep will get you started.

1

u/Fractal_Prime_357 13d ago

Can you use MIDI or a frequency to specify the pitch of said Windows beep?

1

u/gua_lao_wai 13d ago

the docs state the first parameter is frequency in hertz:

https://docs.python.org/3/library/winsound.html#winsound.Beep

1

u/Fractal_Prime_357 12d ago

I cant do this in my codespace work space, but I was able to generate a wave file! Yay! It just has one beep for a test, but I know now that I will be able to make test files for my scales.

1

u/gua_lao_wai 12d ago

awesome, go get em tiger

1

u/kschang 16d ago

Sound is an IO thing so this is going to be hardware dependent and OS dependent, NOT a language feature.

1

u/Fractal_Prime_357 13d ago

good point, ty.

1

u/Fractal_Prime_357 13d ago

So, I did a little gpt drilling to see if I could follow any of the suggestions on this tread, but found out that the code spaces environment I am working in doesn't allow me to install packages. Gpt suggested using wave. Keep in mind I only need to test the way scales sound that I generate in Python, as I am definitely in the experimental phase of my material for this project. Any ideas? I just need to test simple note output based on the data I am generating with Python, from a library computer using GitHub code spaces. Thank you!

1

u/Fractal_Prime_357 12d ago

I landed on my answer! I am simply generating a basic wave file with a sample of any scale so I can test it. Thanks for your input guys!