I would like to do basic audio mixing in python.
To give an example: I would like to take two mp3 files and add them together and return one mp3 file. Another example: I would like to take the first ten seconds of one mp3 file and add it to the beginning of another mp3 file.
What is the best way to accomplish these tasks? I would like to use built in python functions like audioop but can not find any good tutorials or sample code out there for using the built in functions.
I am going through the docs but I am pretty confused and cannot figure out how to do things like this. I am not even sure that the python libraries like mp3's. Most of the stuff I have looked at seem to refer to WAV files. So, if this is the case, I guess a follow up question would be is there an easy way to convert an mp3 to a WAV for manipulation and back again?
You can do this pretty easily using pydub:
from pydub import AudioSegment
sound1 = AudioSegment.from_mp3("/path/to/file1.mp3")
sound2 = AudioSegment.from_mp3("/path/to/file1.mp3")
# mix sound2 with sound1, starting at 5000ms into sound1)
output = sound1.overlay(sound2, position=5000)
# save the result
output.export("mixed_sounds.mp3", format="mp3")
You could check out some of the code in the python audiotools project. It is a collection of command-line utilities which make use of a common python package. There is a utility included with audiotools (trackcat) which can con*cat*enate two or more audio tracks; another (tracksplit) can split an audio track (using a .cue file). These, as well as the numerous other included utilities, can work with audio files of various encodings, including mp3.
The way I've done this in the past is just use subprocess. and call sox.
E.g. subprocess.call(["sox", "in.1.mp3", "in.2.mp3", "out.mp3"])
Related
For my final year project in college I am working with Wav Files and Python and messing around with them. I would love to be able to play the sound samples from memory rather than write the sound samples out in to a WAV file before I can hear them.
I have been looking for weeks online and have found PyMedia, PySound, PyGame etc and none of them seem to work for me. Every single package gives me errors.
Are there other libraries I am missing that would help me do this ? Or am I just being stupid and can't get the other packages to work.
Exactly what I want to do is along the lines of this:
#open file and get parameters
wavfile = Wave.open("file.wav", "r")
params = wfile.getparams()
nframes = params[3]
#get sound samples in a list
samples = []
for i in range(nframes):
samples.append(wfile.readframes(1))
playsound(samples)
changedSamples = makeChangeTo(samples)
playsound(changedSamples)
And I would like to be able to have this in a loop so I can edit and hear the edits while the program is still runnning without having to write the samples to a wav file before being able to hear it as that takes too long.
Any suggestions ? Cheers !
You should clearly separate those two concerns:
Reading/writing WAV files (or other audio files)
Playing/recording sounds
There are several questions and answers to both topics here on SO.
This is my personal (and of course biased) recommendation:
You should use NumPy to manipulate sounds, it's much easier than handling plain Python buffers.
If for some reason you cannot use NumPy, you can still do all this, but it will be a bit more work.
For reading/writing sound files, I recommend the soundfile module (full disclosure: I'm a co-author).
For playing/recording sounds, I recommend the sounddevice module (full disclosure: I'm its main author).
When using those modules, your example would probably become something like this:
import soundfile as sf
import sounddevice as sd
samples, samplerate = sf.read('file.wav')
sd.play(samples, samplerate)
sd.wait()
changed_samples = make_change_to(samples)
sd.play(changed_samples, samplerate)
sd.wait()
If you work in an interactive Python prompt, you probably don't need the sd.wait() calls, you can just wait until the playback has finished. Or, if you get bored of listening to it, you can use:
sd.stop()
If you know that you will use the same sampling rate for some time, you can set it as default:
sd.default.samplerate = 48000
After that, you can drop the samplerate argument when using play():
sd.play(samples)
If you want to store the changed sound to a file, you can use something like this:
sf.write('changed_file.wav', changed_samples, samplerate)
Further reading:
different options for reading/writing audio files
different options for playback/recording
a very basic tutorial about handling audio signals
I have an audio track in AIFF format. I would like to open this audio file with Python, and import the amplitudes of the sound and perform some mathematical analysis such as Fourier Transform, etc.
Is this possible in Python?
Are there libraries or modules, which allow me to acquire an audio file?
Throughout my search, I have found scipy.io.wavfile, which works for WAV audio files.
Are there other libraries to import audio files in Python?
Is there something similar for AIFF files?
Obviously, I can convert the AIFF into a WAV file, but I would like to import the AIFF file directly, if possible.
As a side question: are there some more specific (by specific, I mean better than Python) programming languages to perform such kind of analysis and acquisition of audio files?
Python comes with AIFF support as part of the standard library -- see the aifc module.
This module provides support for reading and writing AIFF and AIFF-C
files. AIFF is Audio Interchange File Format, a format for storing
digital audio samples in a file. AIFF-C is a newer version of the
format that includes the ability to compress the audio data.
Depending on what your end goals are, you may be more productive using a tool like PureData that's designed just for working with audio and has things like reading audio files and performing ffts as primitives.
Yes, I also came across this problem using scipy.io.wavfile. I looked up the problem and see that Scikits might be interesting to get around this wave only solution.
https://sites.google.com/site/ldpyproject/scikits-audiolab
As for Pure Data I use this a lot, but of course it does depend on what you wishing to do with your sound file...?
I use Windows 7. All I want to do is create raw audio and stream it to a speaker. After that, I want to create classes that can generate sine progressions (basically, a tone that slowly gets more and more shrill). After that, I want to put my raw audio into audio codecs and containers like .WAV and .MP3 without going insane. How might I be able to achieve this in Python without using dependencies that don't come with a standard install?
I looked up a great deal of files, descriptions, and related questions from here and all over the internet. I read about PCM and ADPCM, as well as A/D Converters. Where I get lost is somewhere between the ratio of byte input --> Kbps output, and all that stuff.
Really, all I want is for somebody to please be able to point me in the right direction to learn the audio formats precisely, and how to use them in Python (but first I want to start with raw audio).
This questions really has 2 parts:
How do I generate audio signals
How do I play audio signals through the speakers.
I wrote a simple wrapper around the python std lib's wave module, called pydub, which you can look at (on github) as a point of reference for how to manipulate raw audio data.
I generally just export the audio data to a file and then play it using VLC player. IMHO there's no reason to write a bunch of code to playback audio unless you're making a synthesizer or a game or some other realtime app.
Anyway, I hope that helps you get started :)
Now, I have to split a mp3 file into several parts. I use python to do the work. But I cannot find a good library to do this job. I've already tried pymp3cut, echo-nest-remix, but it cannot work well.
So is there any better choice?
You could convert your mp3s to raw .wav and use audiolab to read it in, then write out files every N frames, then convert those to mp3.
I have been using VLC's python binding http://wiki.videolan.org/Python_bindings to convert videos and split into frames. VLC supports MP3 but I must admit I have not used it with audio.
I am looking for a high level audio library that supports crossfading for python (and that works in linux). In fact crossfading a song and saving it is about the only thing I need.
I tried pyechonest but I find it really slow. Working with multiple songs at the same time is hard on memory too (I tried to crossfade about 10 songs in one, but I got out of memory errors and my script was using 1.4Gb of memory). So now I'm looking for something else that works with python.
I have no idea if there exists anything like that, if not, are there good command line tools for this, I could write a wrapper for the tool.
A list of Python sound libraries.
Play a Sound with Python
PyGame or Snack would work, but for this, I'd use something like audioop.
— basic first steps here : merge background audio file
A scriptable solution using external tools AviSynth and avs2wav or WAVI:
Create an AviSynth script file:
test.avs
v=ColorBars()
a1=WAVSource("audio1.wav").FadeOut(50)
a2=WAVSource("audio2.wav").Reverse.FadeOut(50).Reverse
AudioDub(v,a1+a2)
Script fades out on audio1 stores that in a1 then fades in on audio2 and stores that in a2.
a1 & a2 are concatenated and then dubbed with a Colorbar screen pattern to make a video.
You can't just work with audio alone - a valid video must be generated.
I kept the script as simple as possible for demonstration purposes. Google for more details on audio processing via AviSynth.
Now using avs2wav (or WAVI) you can render the audio:
avs2wav.exe test.avs combined.wav
or
wavi.exe test.avs combined.wav
Good luck!
Some references:
How to edit with Avisynth
AviSynth filters reference