question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AudioSegment.overlay() slow when called many times

See original GitHub issue

Hello!

I’m currently working on a tool that renders Note Block Studio songs (.nbs) to audio files. This format is similar to MIDI in that it contains what notes should be played at a given moment, each one with a certain pitch, volume and panning.

Thanks to Pydub, I got this to work flawlessly in less than two days. However, the songs made in this program may easily reach 20,000+ notes, which means calling AudioSegment.overlay() thousands of times. Of course, this slows down everything.

A song with 20k notes takes about 2.5 hours to complete, while a black-MIDI-type song with 150k notes took nearly 14 hours. When removing the overlay() call, however, everything is done within seconds (even with all the pitch/gain/speed changes).

I know the aim of Pydub is not to be highly efficient, but intuitive. What I’d like to know is if there’s any way around the overlay() method that still allows me to keep the other modifications done with Pydub. More specifically:

  • When inspecting the source code, I noticed this method is implemented in pure Python. Could it be made more efficient by doing lower-level calls, such as calling ffmpeg to overlay the audio? (I’d be happy to give this a go if it turns out to be possible.)
  • Should I use AudioSegment.get_array_of_samples() on each sound and use something else to overlay them? Or…
  • Is what I’m trying to accomplish too advanced for Pydub, and I should use a lower-level package instead (e.g. pysox)?

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Peda1996commented, Aug 26, 2021

Thanks a lot 😄.

1reaction
Bentroencommented, Aug 26, 2021

@Peda1996 Yeah, here it is! https://gist.github.com/Bentroen/4df9b8d5d052f9d14bc1a8531fe49994

Before calling to_audio_segment(), make sure all your sounds have the same sample rate by sync()ing them previously if necessary, otherwise, a MemoryError may occur as it tries to find a common sample rate. 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

pydub operating direct to disk vs in memory? #51 - GitHub
I appreciate that disk IO is going to be a whole lot slower, ... audioop calls necessary to do the overlay operation. note:...
Read more >
audio - Python - Overlay more than 3 WAV files end to end
I am trying to overlap the end of 1 wav file with 20% of the start of the next file. Like this, there...
Read more >
audiosegment module
Returns a list of AudioSegments, each of which is all the sound during this AudioSegment's duration from a particular source. That is, if...
Read more >
Pydub module very slow compared to CPython implementation
AudioSegment.from_file(path) for path in PATHS) # Create a silent audio segment to easily overlay the other segments composition = pydub.
Read more >
Python AudioSegment Examples
AudioSegment extracted from open source projects. ... Namespace/Package Name: pydub ... just maybe not on time to avoid # this error a few...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found