@@ -189,10 +189,30 @@ public void stop() {
189189 public void dismiss () {
190190 if (mLastVoiceHeardMillis != Long .MAX_VALUE ) {
191191 mLastVoiceHeardMillis = Long .MAX_VALUE ;
192- mCallback .onVoiceEnd ();
192+ // mCallback.onVoiceEnd();
193193 }
194194 }
195195
196+ public void end () {
197+ //convert the relevant portion of the circular mBuffer to a normal array
198+ int voiceLength = getMBufferRangeSize (startVoiceIndex , tailIndex );
199+ float [] data = new float [voiceLength ];
200+ int circularIndex = startVoiceIndex ;
201+ for (int i =0 ; i <voiceLength ; i ++){
202+ data [i ] = mBuffer [circularIndex ];
203+ if (circularIndex < mBuffer .length -1 ){
204+ circularIndex ++;
205+ }else {
206+ circularIndex = 0 ;
207+ }
208+ }
209+ mCallback .onVoice (data , voiceLength );
210+ //reset relevant variables
211+ startVoiceIndex = 0 ; //is not necessary
212+ mLastVoiceHeardMillis = Long .MAX_VALUE ;
213+ mCallback .onVoiceEnd ();
214+ }
215+
196216 public void destroy (){
197217 if (useBluetoothHeadset ) {
198218 audioManager .unregisterAudioDeviceCallback (audioDeviceCallback );
@@ -302,83 +322,63 @@ public void run() {
302322 }
303323 }
304324 }
325+ }
305326
306- private void end () {
307- //convert the relevant portion of the circular mBuffer to a normal array
308- int voiceLength = getMBufferRangeSize (startVoiceIndex , tailIndex );
309- float [] data = new float [voiceLength ];
310- int circularIndex = startVoiceIndex ;
311- for (int i =0 ; i <voiceLength ; i ++){
312- data [i ] = mBuffer [circularIndex ];
313- if (circularIndex < mBuffer .length -1 ){
314- circularIndex ++;
315- }else {
316- circularIndex = 0 ;
317- }
318- }
319- mCallback .onVoice (data , voiceLength );
320- //reset relevant variables
321- startVoiceIndex = 0 ; //is not necessary
322- mLastVoiceHeardMillis = Long .MAX_VALUE ;
323- mCallback .onVoiceEnd ();
324- }
327+ private int getMBufferSize (){
328+ return getMBufferRangeSize (headIndex , tailIndex );
329+ }
325330
326- private int getMBufferSize (){
327- return getMBufferRangeSize (headIndex , tailIndex );
328- }
329-
330- private int getMBufferRangeSize (int begin , int end ){
331- if (begin <= end ){
332- return end - begin ;
333- }else { //(begin > end)
334- return (mBuffer .length -begin ) + end ;
335- }
331+ private int getMBufferRangeSize (int begin , int end ){
332+ if (begin <= end ){
333+ return end - begin ;
334+ }else { //(begin > end)
335+ return (mBuffer .length -begin ) + end ;
336336 }
337+ }
337338
338- private boolean isHearingVoice (byte [] buffer , int size ) { //old method to measure threshold (not used)
339- for (int i = 0 ; i < size - 1 ; i += 2 ) {
340- // The buffer has LINEAR16 (2 bytes) in little endian.
341- // Therefore, to take the integer value at position i, we convert the (i+1)-th byte into an integer (positive),
342- // shift it to the left by 8 bits and add to it the absolute integer value of the i-th byte
343- int s = buffer [i + 1 ];
344- if (s < 0 ) s *= -1 ;
345- s <<= 8 ;
346- s += Math .abs (buffer [i ]);
347- //if the value is grater than the threshold the method returns true
348- int amplitudeThreshold = global .getAmplitudeThreshold ();
349- if (s > amplitudeThreshold ) {
350- return true ;
351- }
339+ private boolean isHearingVoice (byte [] buffer , int size ) { //old method to measure threshold (not used)
340+ for (int i = 0 ; i < size - 1 ; i += 2 ) {
341+ // The buffer has LINEAR16 (2 bytes) in little endian.
342+ // Therefore, to take the integer value at position i, we convert the (i+1)-th byte into an integer (positive),
343+ // shift it to the left by 8 bits and add to it the absolute integer value of the i-th byte
344+ int s = buffer [i + 1 ];
345+ if (s < 0 ) s *= -1 ;
346+ s <<= 8 ;
347+ s += Math .abs (buffer [i ]);
348+ //if the value is grater than the threshold the method returns true
349+ int amplitudeThreshold = global .getAmplitudeThreshold ();
350+ if (s > amplitudeThreshold ) {
351+ return true ;
352352 }
353- return false ;
354353 }
354+ return false ;
355+ }
355356
356- private boolean isHearingVoice (float [] buffer , int begin , int end ) {
357- // We iterate circlularly the mBuffer from the begin index to the end index, and if one of the values exceed the threshold the method returns true.
358- // Also The range with the old ENCODING_PCM_16BIT was [-32768, 32767], while now with the new ENCODING_PCM_FLOAT it is [-1, 1],
359- // so to convert the values of the new range into those of the old range (the threshold is based on the old values) I have to multiply them by 32767.
360- int numberOfThreshold = 15 ;
361- int count = begin ;
362- while (count != end ){
363- float s = Math .abs (buffer [count ]) * 32767 ;
364- int amplitudeThreshold = global .getAmplitudeThreshold ();
365- if (s > amplitudeThreshold ) {
366- numberOfThreshold --;
367- //return true;
368- }
369- if (count < buffer .length -1 ){
370- count ++;
371- }else {
372- count = 0 ;
373- }
357+ private boolean isHearingVoice (float [] buffer , int begin , int end ) {
358+ // We iterate circlularly the mBuffer from the begin index to the end index, and if one of the values exceed the threshold the method returns true.
359+ // Also The range with the old ENCODING_PCM_16BIT was [-32768, 32767], while now with the new ENCODING_PCM_FLOAT it is [-1, 1],
360+ // so to convert the values of the new range into those of the old range (the threshold is based on the old values) I have to multiply them by 32767.
361+ int numberOfThreshold = 15 ;
362+ int count = begin ;
363+ while (count != end ){
364+ float s = Math .abs (buffer [count ]) * 32767 ;
365+ int amplitudeThreshold = global .getAmplitudeThreshold ();
366+ if (s > amplitudeThreshold ) {
367+ numberOfThreshold --;
368+ //return true;
374369 }
375- if ( numberOfThreshold <= 0 ){
376- return true ;
370+ if ( count < buffer . length - 1 ){
371+ count ++ ;
377372 }else {
378- return false ;
373+ count = 0 ;
379374 }
380- //return false;
381375 }
376+ if (numberOfThreshold <= 0 ){
377+ return true ;
378+ }else {
379+ return false ;
380+ }
381+ //return false;
382382 }
383383
384384 public boolean isOnHeadsetSco (){
0 commit comments