6

I've done alot of searching to try and find a way to achieve this but the solutions I've found either don't do what I need or I don't understand them.

I'm looking for a way of playing a sound in python (non-blocking) that allows me to change the playback speed in real time, as it's playing, with no gaps or cutouts.

Changing the pitch is fine. Audio quality isn't even that important.

Most of the solutions I've found only allow setting the playback speed once, before the file is played.

3
  • Does Python have a way to play or stream PCM values directly? I know of a generally method for changing pitch but it requires that the system allows you access to the signal values. IDK if Python offers this. Commented Apr 5, 2022 at 6:57
  • 1
    Well, you can load wav files into a numpy array. And you can iterate through the array in chunks. Commented Apr 5, 2022 at 8:04
  • 1
    @PhilFreihofner I’ve seen your comment here stackoverflow.com/questions/44818752/… . It is useful and I should really get in there and get my hands dirty! But I think I’ve found a cheap workaround Commented Apr 5, 2022 at 8:49

2 Answers 2

1

I have done this in my python project. I use VLC to present audio/video within a PyQt5 GUI. The GUI has controls to slow or speed the A/V while it is playing. https://github.com/ccbogel/QualCoder The main python file for displaying audio/video is in: view_av.py and in there is the Class DialogViewAV It imports some additional modules for the GUI and vlc integration, if it helps you get your head around it and extract the bits of code you need.

I have a video explaining using the A/V part of the software and at 45 seconds in I mention the playback speed changing option. https://www.youtube.com/watch?v=TjOfPvvXh7U&t=0s

If I get time, maybe, I can prepare a script to do what you are after.

A/V controls

1
  • Thanks for this. I did try Pyqt5 and got it to work but the way I was doing it, it stopped the audio for 1/4 second every time you changed the speed. Not sure why. Tbh honest if I'm going to use VLC and Pyqt5 I may as well stick with python-mpv and mpv, at least until I discover an issue. Commented Apr 5, 2022 at 20:02
0

I've found a solution, using python-mpv, a wrapper for mpv.io

from pynput.keyboard import Key, Listener
import mpv
speed=1

#quick function to change speed via keyboard. 
def on_press(key):

    global speed

    if key.char == 'f' :
        speed=speed-0.1
        player.speed=speed
    if key.char == 'g' :
        speed=speed+0.1
        player.speed=speed

player = mpv.MPV(ytdl=True)
player.play('/Users/regvardy/mediapipe_faceswap-main/test.wav')
with Listener(
        on_press=on_press) as listener:
    listener.join()
while True:
    
    player.speed=speed

I haven't tested it for stability yet.

It feels like a workaround rather than me actually finding out how to do it so I may try and find a different solution.

1
  • Another solution appears to be using pyrubberband but I couldn't get it to work on my system Commented Apr 5, 2022 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.