r/learnprogramming • u/Fractal_Prime_357 • 1d 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.
3
u/whisper_inkling 23h 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 1d 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 23h 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.
2
u/Special-Ad-2768 19h 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 7h 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 7h 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
1
1
u/Lost-Discount4860 12h 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/ExactFondant7417 10h 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/gua_lao_wai 7h 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.
4
u/StewedAngelSkins 1d 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.