I'm trying to write something that catches the audio being played to the speakers/headphones/soundcard and see whether it is playing and what the longest silence is. This is to test an application that is being executed and to see if it stops playing audio after a certain point, as such i don't actually need to really know what the audio itself is, just whether or not there is audio playing.
I need this to be fully programmatic (so not requiring the use of GUI tools or the like, to set up an environment). I know applications like projectM do this, I just can't for the life of me find anything anywhere that denotes how.
An audio level meter would also work for this, as would ossiliscope data or the like, really would take any recommendation.
Here is a very similar question: record output sound in python
You could try to route your output to a new device with jack and record this with portaudio. There are Python Bindings for portaudio called pyaudio and for jack called PyJack. I have never used the latter one but pyaudio works great.
Related
What I need
I am working on a music player with a sample analyzer. My issue is that I am currently using pydub to get the samples from the song, but as the song gets longer, the more out of sync the two become. I have made the two literally in sync, pydub just so happens to read out of it [I even started them at the same time, the two actually progressively de-synced with audio alone and more so as the song continues playing].
Basically, is there a python-vlc equivalent to pydub.AudioSegment.get_array_of_samples()?
You can look at the python-vlc docs here: https://www.olivieraubert.net/vlc/python-ctypes/doc/
What I've tried
python-vlc has sound.get_time() which returns the last updated time in milliseconds. The issue with this command is not that it only updates every half a second or so [I've found a way around that] but that it doesn't return the accurate time. I can start a timer from when it starts playing using time.monotonic(). As time progresses, the get_time() wildly varies from the timer. I've gotten a difference of 195ms, 294ms and 217ms.
I tried using an external timer using the threading module. It appears that pydub.AudioSegment[index] is not in sync whatsoever.
Scaling from sound.get_time() in py-vlc to len(sound) in pydub. This doesn't work as expected. I cannot tell you why but it is still desynced.
Using sound.get_time() with an average offset that increases over time. It appears that pydub.AudioSegment[index] doesn't line up properly.
Using aubio. It only reads WAV files, and for a live analyzer, converting to a WAV first would take too long.
Things I found out
I've looked at how long each song is in milliseconds, len(sound) for pydub and sound.get_length() for python-vlc is different usually by 10 or so seconds.
Things that won't work
Using pydub's play() command. I don't like it because it's very limiting.
Using something else than py-vlc to play the audio, VLC has many features that just cannot be replicated.
Previous suggestions
Use How can I extract audio from video with ffmpeg?
This won't work because I am not looking to extract audio from an existing video file, I'm trying to get samples like pydub.AudioSegment.get_array_of_samples()
I'm curious
Is there a python module for Audacity? I've been looking and couldn't find it. I can also use some other command line tool that can interact with Audacity if possible [I know the built in command line utility doesn't do much at all]
You probably want to use ffmpeg directly for this.
I want to write a program in Python which plays random music. Is there any library that can directly play a sound of the given pitch or frequency, during the given time? For example, play(440, 4) should play the note A during 4 seconds.
I've found some libraries that can do what I said, but they don't play the music directly: you have to create a .wav file and, then, play this file. If I don't find any library that does what I want, I'll create a file, play it, and remove it for each note, but I think it would be more easy to do it directly.
I've finally found an answer to my question.
There's a module called winsound (https://docs.python.org/3/library/winsound.html) which can play a sound of a given frequency and duration: winsound.Beep(frequency, duration). But the problem is that it isn't cross-platform, only works for Windows operating systems.
So I've found another module called pyaudiere (https://pypi.org/project/pyaudiere/) which is cross-platform. It works a bit different than winsound. First you create a device: d = audiere.open_device(). Then, you can create a tone doing t = d.create_tone(440). To play the tone you do t.play() and to stop it t.stop(). If you want to play it during some seconds you can use the time module. I'm still investigating but I think it only works for Python2. This is the stackoverflow post where I found the answer: Python library for playing fixed-frequency sound.
I need to play several videos (with the relative audios) under a python script because I need to insert specific pauses between them and triggers (for a neuropsychological experiment).
I tried many examples on the internet to implement the media player, like this one from here, but I didn`t manage to compile the library MPlayer.
Finally I just need to have videos controlled by python, either using an external program or not. The critical part is to find a way to implement playvideo() in the following code
for pairs in Videos_paired_to_play:
video1,video2 = pairs
send_trigger(triggerType1)
playvideo(video1[1] )
make_a_pause(2)
playvideo(video2[1])
send_trigger(triggerType2)
make_a_pause(20)
Ideally, I would need a way to play videos without destroying the window after having played them.
Thanks!
If you're on windows, you could tie into the windows media player COM object which will have a lot more options available to you
https://msdn.microsoft.com/en-us/library/windows/desktop/dd564035(v=vs.85).aspx
Alternatively, you could find another feature-complete player that exposes it's Interface to COM or python depending on OS. This would probably beat trying to hack your own, and the documentation would be there if you needed to extend or refactor it for other things
I am learning python. I thought of a fun and do-able(for me as a noob) exercise to test myself with.
I want to write a script that asks the user for a string.. and then it translates the string into morse code and plays it. I just dont know how to access/play a sound file in Python... Doesnt really matter what type.. .wav .mp3 or whatever, just whatever works best with Python. It obviously will just be a short beep that will be played for a certain(very short) time length. I saw that people suggest using pygame.. is that the only option? For educational purposes I'd like to know how to do this with the standard library(if possible). If not a module is fine.
Or better yet, is there a way to create a beep sound with only python code? In other words no sound file needed at all. A function that says "make a sound at x frequency for x time"?
You could try the winsound library (built in).
By using winsound.PlaySound(filename,winsound.SND_FILENAME) you are able to play WAV audio files.
By using winsound.Beep(frequency(in Hz),duration(in miliseconds)) you should be able to produce a beeping sound.
There is a slight chance the Beep() function won't work, as is in my case, and i am currently looking into it.
Take a look at the docs here: https://docs.python.org/3.7/library/winsound.html
Is it possible to get the system output audio (the exact same thing that goes through the speakers) and analyze it in real time with Python? My intention is to build a sound visualizer. I know that it is possible to access the microphone with pyaudio, but I was not able to access the sound card output in any way, I'm looking for a solution that works on Windows.
Thank you for reading.
Not sure how this project is doing these days, it's been a long time since it's been updated. PyVST allows you to run python code in a VST inside a VST host, which makes it possible to handle realtime audio events.
You might want to look at http://code.google.com/p/pyo/ for some ideas about how to handle DSP data as well.