Android Question infinite loop

k.m

New Member
hello,

i need an infinite loop like arduino loops that do somthings for specified time without freezing app.
i need to get mic samples in a loop for 20ms and then calc average of samples like this arduino code :
C:
            start = millis(); // millis()  Returns the number of milliseconds passed since the Arduino board began running the current program  in arduino
            reader = 0 ;
            n = 0 ;
            while (millis() < start + 20 )
             {
                  reader = reader + A.AudioMaxAmplitude/2700.0 ;
                  n = n + 1 ;
              }
            reader = reader / n  ;     // I got an average



please, help me .

regards
 

Midimaster

Active Member
Licensed User
The AudioStreamer() liberary offers what you are looking for. it takes the Microphone recording and buffers the samples in a Byte()-Array. Then you will need the ByteConverter()-Library to convert them into Short()-Arrays. The ByteConverter.LittleEndian must set to true.

In combination with a Timer you can fetch every 20msec what the AudioStreamer() buffered for you. The length (in samples) will differ, because the timer is not exact enough. So you have to calculate the average from the number of samples that the AudioStreamer delivered.

The AudioStreamer Object is placed in the STARTER-module. The ByteConverter and your code belongs to the MAIN module.

This is completely async. So you do not wait in a DoWhile-Loop as on arduino, but fetch the sample chunks every 20msec from the AudioStreamer. The converting and your calculation of each chunk afterwards will last roundabout <5msec only.

Perhaps you will have a look on my ringbuffer tutorial, where I also use the AudioStreamer (but for Playback). But the procedures how to access the AudioStreamer are nearly the same you will need. https://www.b4x.com/android/forum/threads/audio-ringbuffer.122761/
 
Last edited:
Upvote 0
Top