On Windows, this is called stereo mix and can be found in Volume Control, under audio input. Now, you want to play audio to the recording stream, so that any application recording the audio input records the stuff that you are playing. I will leave first answer intact as you need that part too. Okay, as we clarified what is really you need here is the answer. There are some more possibilities, including pymedia, but I consider these the easiest solutions. Install PyAudio and some library that decodes Mp3, there are quite a few of them on, and use it to decode the MP3 and feed the output to PyAudio. Install pygame and use it to play MP3 directly It has mostly educational value and it is used in cases where you do not need real-time speed. It can be optimized for nearly full speed, but nobody is interested in doing so. There is pure Python MP3 decoder implementation, but it is 10 times slower than necessary for real-time audio playback. or exploit gstreamer.).īut why go through all this just to avoid dependencies, when you have nice libraries out there with simple interfaces to access and use audio devices.īut, playing MP3 without external libraries, in real time, from pure Python, well, it's not exactly impossible, but it is very hard to achieve, and as far as I know nobody even tried doing it. It is not hard, but to do it you have to use ctypes on Windows, PyObjC on Mac and Linux is special case as it supports many audio systems (probably use sockets to connect to PulseAudio or pipe to some process like aplay/paplay/mpeg123. Then implement a nice set of functions for choosing an audio device, setting up properties like sample rate, bit rate, mono/stereo., feeding the stream to audio card and stopping the playback. To play raw audio data from Python without installing pyaudio or pygame or similar, you first have to know the platform on which your script will be run. Well, playing uncompressed audio is, but MP3, well, I'll explain below. Is it possible, without any dependencies, yes it is, but it is not worth it. If you meant how to play MP3 using Python, well, this is a broad question. PPS: I'm still trying to figure out a way to pipe your own mic through there as well since this method will obviously not pipe your real microphone in too, but looking into the source code of pygame is making my head hurt due to it all being written in C. PS: This took me a while to figure out, your welcome. (Also if you want it to play on a button press I recommend using the python library keyboard, the GitHub documentation is really good and you should be able to figure it out on your own.) > ("Megalovania.mp3") #Load the mp3Īlso, the music doesn't play through your speakers, so you're going to have another python script or thread running that handles playing it through your speakers. > mixer.init(devicename='CABLE Input (VB-Audio Virtual Cable)') #Initialize it with the correct device > mixer.quit() #Quit the mixer as it's initialized on your main playback device > mixer.init() #Initialize the mixer, this will allow the next command to work > from pygame import mixer #Playing sound Then: > from pygame._sdl2 import get_num_audio_devices, get_audio_device_name #Get playback device names Then run this command in cmd: pip install pygame=2.0.0.dev8 (or py -m pip install pygame=2.0.0.dev8, depending on your installation of python) [Also the reason it's the dev version is that it requires some functions only in sdl2, whereas the main branch uses sdl1) (Also from what I know this specific answer only works on Windows, but it should be similar on Linux with PulseAudio instead of VB-Audio Cable, but I'm not a daily Linux user so I don't know.)įirst download:, this will create a "Virtual Audio Cable" where programs can play music to the input device (What looks like a speaker) and it'll pipe it to the output device (What looks like a microphone). It is possible but it isn't 100% in python as it requires the installation of other software.
0 Comments
Leave a Reply. |