igorski / MWEngine

Audio engine and DSP library for Android, written in C++ providing low latency performance within a musical context, while providing a Java/Kotlin API. Supports both OpenSL and AAudio.
MIT License
259 stars 45 forks source link

Sometime sound is distorted at the beginning of the loop #89

Closed harthorst closed 5 years ago

harthorst commented 5 years ago

I have a sequence of sample events running in an infinite loop. Sometimes I hear a distortion when the loop returns to the beginning. Sometimes not. This happens randomly. I don't change speed nor do I do anything else. It's just the engine running.

Do you have any hints about that? I tried the limiter but it does not help here.

teotigraphix commented 5 years ago

Does it sound like a buffer clip/crunch?

harthorst commented 5 years ago

For me it sounds like clipping. But it's hard to say because the sound is very short. The weird thing is that the same note most of the time sounds right.

harthorst commented 5 years ago

I prepared a record: https://wetransfer.com/downloads/ddc859af5039dfadb10f1ee16dfe28b020181122180033/c10d20afdce54f0d06d3de76a0111b7320181122180033/1bd165

It happens at 8.05 - there the loop is repeated. It does not seem to be the roll before - if I move that pattern to the middle the glitch still is there at 8.05.

igorski commented 5 years ago

Seems like a clip that occurs due to a large DC offset jump. Out of interest: what is the sample rate and the buffer size the engine is configured at and what is the tempo ?

Also, did you record the output of your device or use the bouncing function of the engine ?

harthorst commented 5 years ago

I'll analyze the samples regarding DC offsets. The sample at 8.05 is used several times (at every 1) with the same volume and pitch. I wonder why the clip only happens when sequencer position is 0.

About the samples: they are recorded with an external sound card. Cajon was recorded with one mic in the box and one in front of (mixed together for the app), claps are made with a single recording. Sampling rate is 44k, 24 bit, converted to 16 bit. The recorded rhythm was generated with 206 bpm.

The engine itself is running with default settings (like in the sample application) in my phone it's 48k sample rate. I'll have to connect the debugger to see the buffer size - I'll do this at the weekend. I already experimented with larger buffer sizes but without success.

igorski commented 5 years ago

Cool stuff :) If I have the exact buffer size and sample rate I can quickly set up a unit test just for this scenario. I don't think there's an issue with the samples themselves (or an ideal buffer size) but rather some glitch with the loop offset that might lead to some samples being dropped. Not entirely sure how that occurs as this scenario is under test anyways, I also don't suppose it's a latency issue because it would be strange to occur only when the sequencer starts its loop.

harthorst commented 5 years ago

Update: buffer size is 960. Tempo is: 206.39999389648438, loop range = 0, 334871

igorski commented 5 years ago

Hi @harthorst can you check out the branch in https://github.com/igorski/MWEngine/pull/88 while doing refactorings there I have come across a bug within SampleEvent that would render the wrong content the moment the engine would loop. Given that your loop range for your buffer size leads to between 348/349 iterations the bug could lead to what you experience here (e.g. clipping due to dc offset jumps happening irregularly, but always on loop).

harthorst commented 5 years ago

Hi @igorski . Many thanks. I'll give that a try as soon as possible.

harthorst commented 5 years ago

Hi. Updated the files from the branch. Now the last sample events of the last pattern in a loop are muted.

harthorst commented 5 years ago

I found out the reason. I did not set the playback rate of the sample event. For some reason this worked for most events. Seems like sampleevent.cpp checks the sample rate: if ( _playbackRate == 1.f ) That makes the difference, I think. So with custom sample rates (which I don't have) there still might be a problem.

I'll now check if the issue is gone. It will take a while until I can be sure. The first test sounds very promising.

Many thanks again, @igorski

igorski commented 5 years ago

Note to self: don't offer work-in-progress branches as a possible solution for a different feature. :) The issues you mention with events being muted was a bug introduced while working on said feature branch. The feature has now been finished and merged into master, could you check again whether this addresses your issue (there have been fixes made that affect looping precision) ?

harthorst commented 5 years ago

I'll update to master untill chrismas. Right now I can say: the effect did not occur anymore so the bug is most likely fixed.

igorski commented 5 years ago

Music to my ears! If it occurs again, please reopen this issue.