sinshu / py-meltysynth

A SoundFont MIDI synthesizer written in pure Python
Other
20 stars 1 forks source link

Why not using numpy? #2

Closed Yikai-Liao closed 8 months ago

Yikai-Liao commented 8 months ago

I noticed that no dependency is a feature that you care about. But numpy is basically recognized as the "standard library" of python.

And It could significantly reduce the performance overhead in python if numpy is introduced.

Also, I don't think people care too much if a python library is single-file and has no external dependencies, as long as it can be directly installed by pip.

I could understand that you want py-meltysynth to be a portable and light-weight python library. But you need to understand these two features in the context of python:

To sum up, I think it would be nice if you could introduce numpy into this lib and upload it to pypi.

sinshu commented 8 months ago

Thanks for the comment 😊

I've also considered using NumPy, but I've noticed it's still difficult to achieve decent performance.

There are three main heavy processes in synthesis: oscillator, LPF, and mixing. Improving the mixing (the ArrayMath class) by using NumPy shouldn't be too hard. Improving the LPF (the BiQuadFilter class) is probably possible using SciPy. But the oscillator is not easy, since it requires relatively complex sample-by-sample operations.

Even if the overhead of mixing and LPF went to zero, the overall performance improvement would be something like 50%, and it would still be too heavy to use this library for real-time synthesis. For me, it's enjoyable to watch Python's speed improve year by year, so perhaps this is fine as it is.

If I were to seriously create a synthesizer library for Python, the best approach would undoubtedly be to make RustySynth a Python module using PyO3. This would surely result in a substantial performance improvement, and it would also include reverb and chorus. However, since I rarely use Python myself, I don't have much motivation to go that far 😖

Yikai-Liao commented 8 months ago

Thanks for your prompt reply. I understand. Now We're planing to write a c++ SoundFont MIDI synthesizer for our symbolic music preprocessing library symusic.

While it's not easy to use meltysynth directly, it does give us a lot of inspiration, especially on processing SoundFont. We also plans to use mmap and portable simd (google highway) to accelerate our implementation.

Thanks again for your contribution to the open source community.