iOS Question Faster way to get data from a recording wave file

Cadenzo

Active Member
Licensed User
Longtime User
I am recording a non compressed wave-File with iMedia AudioRecorder and want to analyze it in real time: every 50 milliseconds the latest 2205 Shorts (sample rate 44.100 / 20) from the recorded file with Random Access File. I tried several variants, but got speed problems with it (too slow):
1. raf_Wave.ReadShort(iPos) 'read every single short directly from the Random Access File
2. raf_Wave.ReadBytes(b_Data, 0, i_AnalyseBytes, i_BytesOffset) : i_Data = bc.ShortsFromBytes(b_Data) 'copy Bytes in an ByteArray and than concert it to a shortArray

Is there another variant to get the recorded short-array (latest 2205 shorts from the file) faster? Thank you for your ideas!
 

Cadenzo

Active Member
Licensed User
Longtime User
OK done, thank you for help!
In the code I tested with 250 ms now, the measured time should be about 250 here, +- 10 would not be a problem, but it is more and every time different.
AnalyseLoop started Time: 312 ms Time: 303 ms Time: 305 ms Time: 295 ms Time: 301 ms Time: 290 ms Time: 285 ms Time: 300 ms Time: 279 ms Time: 303 ms Time: 300 ms Time: 310 ms Time: 289 ms Time: 307 ms Time: 284 ms Size: 423186 Byte bei Dauer: 4751 MS Time: 292 ms AnalyseLoop ended
 

Attachments

  • WaveAnalyse.zip
    3.6 KB · Views: 146
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've changed the code to:
B4X:
Sub SetAnalyseData
    'Question here: what is the fastest way to get the just recorded short array?
    Dim n As Long = DateTime.Now
    raf_Wave.ReadBytes(b_Data, 0, i_AnalyseBytes, i_BytesOffset)
    i_Data = bc.ShortsFromBytes(b_Data)
    Log("aaa: " & (DateTime.Now - n))
End Sub

Even in debug mode it takes between 0 to 1 ms. This means that it is very fast.

1. I have a feeling that you made your tests in debug mode. This is a mistake.
2. Better to make the Sleep a bit shorter. Don't assume that it will be raised at the exact millisecond. If this is critical then make it shorter and add a short "busy loop".
 
Upvote 0

Cadenzo

Active Member
Licensed User
Longtime User
Thank you
1. I tested also in release mode, showing analysing result in a label. So I have sometimes a longer delay with updating results. If the reason is not in the part of getting short array data it could be also the analysing algorithm itself, but same code is really fast enough on android. I have to test here...
2. By "busy loop" you mean somthing like Do While DateTime.Now - n < 50 ?

So is i_Data = bc.ShortsFromBytes(b_Data) the recommed way to get short array in time critical situations?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
So is i_Data = bc.ShortsFromBytes(b_Data) the recommed way to get short array in time critical situations?
It is a mistake to discuss the code that takes about 0ms to run.

Don't add the busy loop until you understand which step is slow. I didn't know that you haven't posted all of the code...
 
Upvote 0

Cadenzo

Active Member
Licensed User
Longtime User
I didn't know that you haven't posted all of the code...
I think it was the relevant (reading related ) code. The other part is tested and fast enough. The differences in milliseconds comes already from this posted code. So I thought that it could be influenced by this array transfer, even if it is fast enough, if measured isolated.

But I understand, that after sleep it will not be raised at the exact millisecond. So I will try this "busy loop". Thanks.
 
Upvote 0
Top