mackron / miniaudio

Audio playback and capture library written in C, in a single source file.
https://miniaud.io
Other
4.07k stars 361 forks source link

Add ma_pulsewave Generator Type #675

Closed jaybaird closed 1 year ago

jaybaird commented 1 year ago

Adds a ma_pulsewave generator type to allow a pulse wave with a custom duty cycle. It does not change or break any existing ma_waveform_type_square implementations and is/should be 100% backward-compatible. Under the hood, ma_pulsewave is a thin wrapper around a ma_waveform of type ma_waveform_type_square. The dutyCycle is defaulted to 0.5 for ma_waveform square waves, and for ma_pulsewave is plumbed through accordingly.

Let me know if you'd like to not wrap things like this. It seemed prudent to use as much code as was there as it's been tested and reviewed. I did this to add the API I was looking for but wanted to retain backward compatibility. e.g., a squarewave is a pulse wave, but this inversion allows for the least surface area change.

jaybaird commented 1 year ago

I didn't see an obvious way to update the documentation. If you accept this, let me know how I can help document!

mackron commented 1 year ago

Thanks. Is the only change to the computation this part? https://github.com/mackron/miniaudio/pull/675/files#diff-f31380d18dd03a06089b755f2b5ccd5e568ac32f0b30b240492b75f32cee4e5bL65234-R65260

I'm a bit divided on this. On one hand, it seems like such a trivial change to introduce a whole new type, but on the other hand it does kind of make sense to have a separate pulse wave object since it is a different type waveform. I'll sit on this and have a think about it.

While I consider this, a quick change to your PR - are you able to swap your parameters here: https://github.com/mackron/miniaudio/pull/675/files#diff-f31380d18dd03a06089b755f2b5ccd5e568ac32f0b30b240492b75f32cee4e5bR10121-R10122. The reason for this, is that by having ma_waveform be the first member, it becomes compatible with ma_data_source.

jaybaird commented 1 year ago

Thanks. Is the only change the computation this part? https://github.com/mackron/miniaudio/pull/675/files#diff-f31380d18dd03a06089b755f2b5ccd5e568ac32f0b30b240492b75f32cee4e5bL65234-R65260

That's correct.

mackron commented 1 year ago

I'm happy to merge this. The additional code is only trivial so I don't think it'll add too much maintenance costs.

A very quick and unimportant side note. This PR is missing the declaration of set_duty_cycle(). I'll add it manually post merge.