How to launch two pydub threads in parallel?
Here's the problem:
I want to play two mp3 or mpeg format audio files in parallel using pydub. What I notice is that I can start one thread no problem, but it hangs until the first audio file finishes. Only then does the second file start.
I believe this to be due to the python GIL. So I think the threads will not be able to fire in parallel. Am I correct in thinking this?
Here's a simple version of my code:
from pydub import AudioSegment
from pydub.playback import play
import threading
from threading import Thread
from threading import Event
class Song(Thread):
def __init__(self, filename):
"""initializes the thread"""
Thread.__init__(self)
self.soundfilename = filename
self._stopper = Event()
self.setName('SoundThread')
def run(self):
"""plays a given audio file"""
song = AudioSegment.from_mp3(self.soundfilename)
play(song)
def stop(self):
self._stopper.set()
if __name__ == '__main__':
s1 = '01. Born To Kill.mp3'
s2 = 't1.mpeg'
music1 = Song(s1)
music2 = Song(s2)
music1.run()
music2.run()
The only work around I discovered on the matter entails taking the mp3, converting it to wave, then feeding it to pyaudio on a thread. That's fine, but it might be computationally expensive for my purposes.
Link for workaround
Thank you!
python multithreading pyaudio pydub
add a comment |
Here's the problem:
I want to play two mp3 or mpeg format audio files in parallel using pydub. What I notice is that I can start one thread no problem, but it hangs until the first audio file finishes. Only then does the second file start.
I believe this to be due to the python GIL. So I think the threads will not be able to fire in parallel. Am I correct in thinking this?
Here's a simple version of my code:
from pydub import AudioSegment
from pydub.playback import play
import threading
from threading import Thread
from threading import Event
class Song(Thread):
def __init__(self, filename):
"""initializes the thread"""
Thread.__init__(self)
self.soundfilename = filename
self._stopper = Event()
self.setName('SoundThread')
def run(self):
"""plays a given audio file"""
song = AudioSegment.from_mp3(self.soundfilename)
play(song)
def stop(self):
self._stopper.set()
if __name__ == '__main__':
s1 = '01. Born To Kill.mp3'
s2 = 't1.mpeg'
music1 = Song(s1)
music2 = Song(s2)
music1.run()
music2.run()
The only work around I discovered on the matter entails taking the mp3, converting it to wave, then feeding it to pyaudio on a thread. That's fine, but it might be computationally expensive for my purposes.
Link for workaround
Thank you!
python multithreading pyaudio pydub
You're running therun
method in the main thread. TheSong
thread should be run using thestart
method.
– Kendas
Nov 20 '18 at 10:40
@Kendas, that makes sense. But that start method ultimately calls start. Or am I missing something?
– VladMode
Nov 20 '18 at 14:27
Thestart
method initializes a new thread and runs the contents of therun
method in that thread. calling therun
method on its own, runs the instructions in your main thread.
– Kendas
Nov 21 '18 at 8:50
Verified, the behaviour is as you point out. Thank you again @Kendas for your guidance on this.
– VladMode
Nov 21 '18 at 9:57
add a comment |
Here's the problem:
I want to play two mp3 or mpeg format audio files in parallel using pydub. What I notice is that I can start one thread no problem, but it hangs until the first audio file finishes. Only then does the second file start.
I believe this to be due to the python GIL. So I think the threads will not be able to fire in parallel. Am I correct in thinking this?
Here's a simple version of my code:
from pydub import AudioSegment
from pydub.playback import play
import threading
from threading import Thread
from threading import Event
class Song(Thread):
def __init__(self, filename):
"""initializes the thread"""
Thread.__init__(self)
self.soundfilename = filename
self._stopper = Event()
self.setName('SoundThread')
def run(self):
"""plays a given audio file"""
song = AudioSegment.from_mp3(self.soundfilename)
play(song)
def stop(self):
self._stopper.set()
if __name__ == '__main__':
s1 = '01. Born To Kill.mp3'
s2 = 't1.mpeg'
music1 = Song(s1)
music2 = Song(s2)
music1.run()
music2.run()
The only work around I discovered on the matter entails taking the mp3, converting it to wave, then feeding it to pyaudio on a thread. That's fine, but it might be computationally expensive for my purposes.
Link for workaround
Thank you!
python multithreading pyaudio pydub
Here's the problem:
I want to play two mp3 or mpeg format audio files in parallel using pydub. What I notice is that I can start one thread no problem, but it hangs until the first audio file finishes. Only then does the second file start.
I believe this to be due to the python GIL. So I think the threads will not be able to fire in parallel. Am I correct in thinking this?
Here's a simple version of my code:
from pydub import AudioSegment
from pydub.playback import play
import threading
from threading import Thread
from threading import Event
class Song(Thread):
def __init__(self, filename):
"""initializes the thread"""
Thread.__init__(self)
self.soundfilename = filename
self._stopper = Event()
self.setName('SoundThread')
def run(self):
"""plays a given audio file"""
song = AudioSegment.from_mp3(self.soundfilename)
play(song)
def stop(self):
self._stopper.set()
if __name__ == '__main__':
s1 = '01. Born To Kill.mp3'
s2 = 't1.mpeg'
music1 = Song(s1)
music2 = Song(s2)
music1.run()
music2.run()
The only work around I discovered on the matter entails taking the mp3, converting it to wave, then feeding it to pyaudio on a thread. That's fine, but it might be computationally expensive for my purposes.
Link for workaround
Thank you!
python multithreading pyaudio pydub
python multithreading pyaudio pydub
asked Nov 20 '18 at 10:38


VladModeVladMode
85
85
You're running therun
method in the main thread. TheSong
thread should be run using thestart
method.
– Kendas
Nov 20 '18 at 10:40
@Kendas, that makes sense. But that start method ultimately calls start. Or am I missing something?
– VladMode
Nov 20 '18 at 14:27
Thestart
method initializes a new thread and runs the contents of therun
method in that thread. calling therun
method on its own, runs the instructions in your main thread.
– Kendas
Nov 21 '18 at 8:50
Verified, the behaviour is as you point out. Thank you again @Kendas for your guidance on this.
– VladMode
Nov 21 '18 at 9:57
add a comment |
You're running therun
method in the main thread. TheSong
thread should be run using thestart
method.
– Kendas
Nov 20 '18 at 10:40
@Kendas, that makes sense. But that start method ultimately calls start. Or am I missing something?
– VladMode
Nov 20 '18 at 14:27
Thestart
method initializes a new thread and runs the contents of therun
method in that thread. calling therun
method on its own, runs the instructions in your main thread.
– Kendas
Nov 21 '18 at 8:50
Verified, the behaviour is as you point out. Thank you again @Kendas for your guidance on this.
– VladMode
Nov 21 '18 at 9:57
You're running the
run
method in the main thread. The Song
thread should be run using the start
method.– Kendas
Nov 20 '18 at 10:40
You're running the
run
method in the main thread. The Song
thread should be run using the start
method.– Kendas
Nov 20 '18 at 10:40
@Kendas, that makes sense. But that start method ultimately calls start. Or am I missing something?
– VladMode
Nov 20 '18 at 14:27
@Kendas, that makes sense. But that start method ultimately calls start. Or am I missing something?
– VladMode
Nov 20 '18 at 14:27
The
start
method initializes a new thread and runs the contents of the run
method in that thread. calling the run
method on its own, runs the instructions in your main thread.– Kendas
Nov 21 '18 at 8:50
The
start
method initializes a new thread and runs the contents of the run
method in that thread. calling the run
method on its own, runs the instructions in your main thread.– Kendas
Nov 21 '18 at 8:50
Verified, the behaviour is as you point out. Thank you again @Kendas for your guidance on this.
– VladMode
Nov 21 '18 at 9:57
Verified, the behaviour is as you point out. Thank you again @Kendas for your guidance on this.
– VladMode
Nov 21 '18 at 9:57
add a comment |
1 Answer
1
active
oldest
votes
I recommend using pydub with simpleaudio which supports concurrently playing multiple sounds. For a simple example see pydub.playback._play_with_simpleaudio()
which you can also just use, if you like.
The main pydub.playback.play()
function tries to present the same functionality regardless of which playback method is being used, so it waits for playback to complete, but simpleaudio works fine with concurrent playback (and the _play_with_simpleaudio()
function does not wait until the sound finishes playing)
Thank you Jiaaro! The solution proposed works.
– VladMode
Nov 21 '18 at 9:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391148%2fhow-to-launch-two-pydub-threads-in-parallel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I recommend using pydub with simpleaudio which supports concurrently playing multiple sounds. For a simple example see pydub.playback._play_with_simpleaudio()
which you can also just use, if you like.
The main pydub.playback.play()
function tries to present the same functionality regardless of which playback method is being used, so it waits for playback to complete, but simpleaudio works fine with concurrent playback (and the _play_with_simpleaudio()
function does not wait until the sound finishes playing)
Thank you Jiaaro! The solution proposed works.
– VladMode
Nov 21 '18 at 9:53
add a comment |
I recommend using pydub with simpleaudio which supports concurrently playing multiple sounds. For a simple example see pydub.playback._play_with_simpleaudio()
which you can also just use, if you like.
The main pydub.playback.play()
function tries to present the same functionality regardless of which playback method is being used, so it waits for playback to complete, but simpleaudio works fine with concurrent playback (and the _play_with_simpleaudio()
function does not wait until the sound finishes playing)
Thank you Jiaaro! The solution proposed works.
– VladMode
Nov 21 '18 at 9:53
add a comment |
I recommend using pydub with simpleaudio which supports concurrently playing multiple sounds. For a simple example see pydub.playback._play_with_simpleaudio()
which you can also just use, if you like.
The main pydub.playback.play()
function tries to present the same functionality regardless of which playback method is being used, so it waits for playback to complete, but simpleaudio works fine with concurrent playback (and the _play_with_simpleaudio()
function does not wait until the sound finishes playing)
I recommend using pydub with simpleaudio which supports concurrently playing multiple sounds. For a simple example see pydub.playback._play_with_simpleaudio()
which you can also just use, if you like.
The main pydub.playback.play()
function tries to present the same functionality regardless of which playback method is being used, so it waits for playback to complete, but simpleaudio works fine with concurrent playback (and the _play_with_simpleaudio()
function does not wait until the sound finishes playing)
answered Nov 20 '18 at 18:45
JiaaroJiaaro
44.8k32135170
44.8k32135170
Thank you Jiaaro! The solution proposed works.
– VladMode
Nov 21 '18 at 9:53
add a comment |
Thank you Jiaaro! The solution proposed works.
– VladMode
Nov 21 '18 at 9:53
Thank you Jiaaro! The solution proposed works.
– VladMode
Nov 21 '18 at 9:53
Thank you Jiaaro! The solution proposed works.
– VladMode
Nov 21 '18 at 9:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391148%2fhow-to-launch-two-pydub-threads-in-parallel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You're running the
run
method in the main thread. TheSong
thread should be run using thestart
method.– Kendas
Nov 20 '18 at 10:40
@Kendas, that makes sense. But that start method ultimately calls start. Or am I missing something?
– VladMode
Nov 20 '18 at 14:27
The
start
method initializes a new thread and runs the contents of therun
method in that thread. calling therun
method on its own, runs the instructions in your main thread.– Kendas
Nov 21 '18 at 8:50
Verified, the behaviour is as you point out. Thank you again @Kendas for your guidance on this.
– VladMode
Nov 21 '18 at 9:57