Search code examples
pythonaudioinputmp3microphone

Playing mp3 file through microphone with python


Is there a way using python (and not any external software) to play a mp3 file like a microphone input?

For example, I have a mp3 file and with a python script it would play it through my mic so other in a voice room would hear it. As I say it is just an example.

Of course, I have done some research. I found out that I can use a software to create a virtual device and do few things to have the result. But my point is if it is possible without installing software but with some kind of python script?


Solution

  • If you meant how to play MP3 using Python, well, this is a broad question.

    Is it possible, without any dependencies, yes it is, but it is not worth it. Well, playing uncompressed audio is, but MP3, well, I'll explain below.

    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.

    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.

    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... or exploit gstreamer.).

    But 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.

    PyAudio is great one.

    Well, that is your concern.

    But, 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.

    There is pure Python MP3 decoder implementation, but it is 10 times slower than necessary for real-time audio playback. It can be optimized for nearly full speed, but nobody is interested in doing so.

    It has mostly educational value and it is used in cases where you do not need real-time speed.

    This is what you should do:

    Install pygame and use it to play MP3 directly

    or:

    Install PyAudio and some library that decodes Mp3, there are quite a few of them on pypi.python.org, and use it to decode the MP3 and feed the output to PyAudio.

    There are some more possibilities, including pymedia, but I consider these the easiest solutions.

    Okay, as we clarified what is really you need here is the answer.

    I will leave first answer intact as you need that part too.

    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.

    On Windows, this is called stereo mix and can be found in Volume Control, under audio input.

    You choose stereo mix as your default input. Now, when you open an recording app which doesn't select itsown input channel, but uses the selected one (e.g. Skype) , it will record all coming out of your speakers and coming into your mic/line in.

    I am not 100% sure whether this option will appear on all Windows or it is a feature of an audio card you have.

    I am positive that Creative and Realtek audio cards supports it.

    So, research this.

    To select that option from Python, you have to connect to winmm.dll using ctypes and call the appropriate function. I do not know which one and with what arguments.

    If this option is not present in volume control, there is nothing for it but to install a virtual audio card to do the loopback for you.

    There might be such a software that comes packaged in as library so that you can use it from Python or whatever.

    On Linux this should be easy using Pulseaudio. I do not know how, but I know that you can do it, redirect the streams etc. There is tutorial out there somewhere.

    Then you can call that command from Python, to set to this and reset back to normal.

    On Mac, well, I really have no idea, but it should be possible.

    If you want your MP3 to be played only to the recording stream, and not on your speakers at all, well on Windows, you will not be able to do that without a loopback audio device.

    On Linux, I am sure you will be able to do it, and on Mac it should be possible, but how is the Q.

    I currently have no time to sniff around libraries etc. to provide you with some useful code, so you will have to do it yourself. But I hope my directions will help you.