Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
codsw2
Posts: 6
Registered: ‎09-10-2008
My Device: Not Specified
Accepted Solution

Audio Record buffer reset problem

Hi,

I am writing a j2me application where I record an audio stream and process it realtime. I access the audio buffer every two seconds, take recorded data into byte array and reset the buffer (ByteArrayOutputStream). Even though I reset the buffer Blackberry handset ignors the reset command and buffer data is accumilated (I use JDE 4.5). Can somebody please let me know how to sort this problem out.

 

Thank you 

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,677
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: Audio Record buffer reset problem

Can you provide a code snippet that shows how you are doing this?
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
New Developer
codsw2
Posts: 6
Registered: ‎09-10-2008
My Device: Not Specified

Re: Audio Record buffer reset problem

Hi Mark,

 

I am sorry I was out of office due to some illness after posting the question. I am doing realtime processing of sound in my application. Here is part of the code I have been using

 

 

ByteArrayOutputStream _buffer ;

 

void startRecorder( ) throws IOException, MediaException {

   

        _p = Manager.createPlayer("capture://audio");

        _p.realize();

        _p.prefetch();

        _rc = (RecordControl) _p.getControl("RecordControl");

 

 

 

        _buffer = new ByteArrayOutputStream();

 

 try {

_rc.setRecordStream(_buffer);

_rc.startRecord();

_p.start();

_observer.startSleep();

 

} catch (Exception e) {

_observer.failed(e.toString());

}

 

    } 

 

 

 

//Then I set a timer which executes following method in every 2 seconds

 

public void processReceivedSoundBuffer() throws IOException {

 

        byte[] byteArray = _buffer.toByteArray();

       

       _buffer.reset();

 

// This processes the sound buffer accuired

        codebook.extractTimeSeries(byteArray, bufferNumber);

 

    } 

 

I checked the size of the _buffer after each 2 seconds and the size was increasing

 

Then I analysed the content of the buffer and found out that the buffer is not cleared by the _buffer.rest() command  as it is supposed to.

 

I would be thankful if you can point me towards a method for resetting the buffer. 

 

Dham 

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,677
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: Audio Record buffer reset problem

How are you checking the size?  I tried to mirror your implementation and called _buffer.size() when the timer fires (after which I call reset) and found that the size was being reset and did not continually increase.

 

I also used the BlackBerry Simulator included with the version 4.5.0 BlackBerry JDE.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
New Developer
codsw2
Posts: 6
Registered: ‎09-10-2008
My Device: Not Specified

Re: Audio Record buffer reset problem

Hi

I use _buffer.size() just after resetting the buffer. I tested this on 8700 (not in the emulator). I tested this with software version 4.2 and I updated the software to 4.5.0.81 and tried again. Same probles was there.

 

Dham

Please use plain text.
New Developer
codsw2
Posts: 6
Registered: ‎09-10-2008
My Device: Not Specified

Re: Audio Record buffer reset problem

Hi,

Today I tested what you said. Yes it works fine with the simulator. I tested with both 8700 (with JDE 4.2.1) and 8300 (JDE 4.5) simulators. They dont show any accumulation.

But when I run the same midlet in the 8700 device, it is different and the resetting of the buffer is not happening.

Please give me a solution for this problem.

 

Dham 

Please use plain text.
BlackBerry Development Advisor
MSohm
Posts: 14,677
Registered: ‎07-09-2008
My Device: BlackBerry Z30, BlackBerry PlayBook

Re: Audio Record buffer reset problem

I tried this on a BlackBerry 8700 running 4.5.0.81 and wasn't able to reproduce it.  The stream was being reset.

 

You could wrap your timer code in a synchronized block that synchronizes on the ByteArrayOutputStream.  It could be that the recording thread and timer thread are accessing the variable at the same time.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Audio Record buffer reset problem

Generally if you need thread safety you have to provide it yourself and audio players can differ widely on their

access patterns. It isn't hard to write your own stream here and even monitor the access patterns as I finally

had to do for a fake httpconnection class. Certainly you can imagine the implementation

 

reset() {  size=0;  }

write() { ++size; ... }

 

would lend itself to threading hazards.

 

And, be aware of the realtime issues with media and constrained devices.

Please use plain text.
New Developer
codsw2
Posts: 6
Registered: ‎09-10-2008
My Device: Not Specified

Re: Audio Record buffer reset problem

Hi,

 

Thanks a lot for the answers. What I cannot understand is that the same piece of code runs well in the simulator and not in the actual device. I wrote a simple code just for recording, buffer size checking and resetting (without any processing of the recorded buffer) but the problem was there. This decoder I wrote was running fine with sonyericsson JP6, JP7 and in Nokia Series 40 5th ed without any problem.

 

I will try it again, keeping your advice in my mind on Monday  and will post the results in this forum.

 

Thanks again 

 

Please use plain text.
Developer
marchywka
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Audio Record buffer reset problem

It is hard to get an emulator to mimic all the dynamic properties of a target device.

 

Here would be the likely implementation code alternatives

 

int size;

write( byte [] b...) {

int localptr=size;                             

system.arraycopy(mybuf,yourbuf,etc );  

size=localptr+foo;  }

reset() {size=0; }

 

compared to

volatile int size;

 write impl:

for ( int i=0;... ) mybuf[size++]= yourbuf[i];

 

In the latter case, your reset call would do something most of the time. In the former case, it is likely to do nothing

much of the time.  

 

It all depends on the implementation and jvm  This is a common problem with multi-threaded code and normally you

would want the code to "usually work" in the absence of concurrency constructs.

 

See my earlier links to sun.com for concurrency issues.

 

 

 

 

Please use plain text.