Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 989687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:49:58+00:00 2026-05-16T05:49:58+00:00

I think this is a general Java question, but it does involve some Android

  • 0

I think this is a general Java question, but it does involve some Android stuff.

So here’s the deal: I’ve got a JNI wrapped version of MPG123 for Android, and I can pull PCM data from an MP3 file on the SDCard. I’d like to pull a chunk this data, do an analysis on it using another class I wrote, then push it into a queue. Ideally, I’d like the audio to play back with a four second delay. I achieved it with no delay using this code:

public void run() 
{
  // The actual thread where the PCM data gets processed and analyzed
 short[] pcm = new short[ 4096 ];
 short[] zero = new short[ 4096 ];
 Queue<short[]> buffer = new LinkedList<short[]>();

 // Fill the zero buffer
 for( int i = 0; i < 4096; i++ )
  zero[i] = 0;

 // Push back 4 seconds of silence - Note this commented section
 //for( int i = 0; i < 43; i++ )
 // buffer.add( zero );

 // Analyze the whole file 
 while( !isInterrupted() )
 {
  // Grab and analyze data, add events
  int ret = audio.GetPCM( pcm );
  //Log.d( "AudioThread", "GetPCM returns: " + ret );
  if( ret != MPG123.MPG123_DONE )
  {
   // Process the PCM data
   if( bd.Process( pcm ) != BeatDetection.AUDALYSIS_OK )
    Log.v( "AudioThread", "Beat Detection Error!" );

   // Add the data to the PCM buffer
   buffer.add( pcm );
  }

  // Play the audio
  short[] data = buffer.poll();
  if( data != null ) // If we have data left in the buffer
   at.write( data, 0, 4096 ); // Write it to the audio track
  else
   break; // Otherwise we need to exit the loop

  Log.d( "AudioThread", "BufferSize= " + buffer.size() );
 }

 // Debug message
 Log.d( "AudioThread", "Exiting Thread." );

 // Clean stuff up
 bd.Cleanup();
 audio.Cleanup();
 at.stop();
 at.release(); 
 }

That said, when I try to introduce a delay into my queue by adding “4 seconds” worth of 0’s into the queue (Note the commented block), my music plays right away, but starts 4 seconds into the song and plays in realtime until the queue starts to empty where it plays the same chunk over and over until breaking out of the loop. It almost seems as if the queue is acting like a “First In Last Out” queue rather than a “First In First Out” queue.

Perhaps I’m not using a Java Queue correctly? Maybe this is some odd bug with the android AudioTrack?

Thanks!

-Griff

EDIT – I solved the problem. It appears to be a problem with my JNI implementation of GetPCM. It appears that when I copied data into PCM from the JNI level, it set the same data for every instance of PCM pushed back into the queue, setting every PCM entry in the queue to the same frame of PCM data. I solved the problem by changing:

buffer.add( pcm );

to

buffer.add( pcm.clone() );

New Question:
Is this normal Java behavior? Did I implement the JNI function incorrectly?

JNIEXPORT jint JNICALL Java_com_motalenbubble_projectlucid_MPG123_GetPCM(
    JNIEnv* env,
    jobject obj,
    jshortArray data ) // jshortarray already has a size attribute
{
    // Call mpg123_read
    jint  err  = MPG123_OK, length;
    jshort *pcm;
    jsize len = (*env)->GetArrayLength( env, data );

    pcm = (*env)->GetShortArrayElements( env, data, NULL );
    err = mpg123_read( mh, (unsigned char*)pcm, len * sizeof( short ), &length ); // Read length is in bytes - we need in shorts. 
    (*env)->ReleaseShortArrayElements( env, data, pcm, 0 );
    // Annoying logging
    //dprintf(0, "read(%d): %s\n", length, err == MPG123_ERR ? mpg123_strerror(mh) : mpg123_plain_strerror(err)); 
    return err;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:49:58+00:00Added an answer on May 16, 2026 at 5:49 am

    It looks like the role of your JNI function is:

    Pass it an initialized buffer (i.e. a java short array object), and it will be filled with the next chunk of samples from the mp3 decoder.

    For the purpose of doing this task, it appears to be implemented correctly. The function never instantiates a java short array object, and I don’t think it should be expected to.

    I’ll give a description of the problem, although it seems you already understood what was happening.

    In your run() method, for the pcm variable you instantiate a short array once. Every time you call audio.GetPCM( pcm ), you’re giving it the same array object. And then when you call buffer.add( pcm ), it always enqueues a reference to the same array. So, if you call GetPCM, and then call GetPCM again before you have written the frame to the audio device, GetPCM will disastrously overwrite the array.

    You solved the problem with buffer.add( pcm.clone() ) because this always allocates a new array, and adds a reference to that new array to the queue. GetPCM still overwrites the same array (pcm) every time, but it’s not a problem because the queue contains references to unrelated arrays, which were created as copies of previous contents of the pcm array.

    The problem isn’t really related to Android or JNI; GetPCM could equally be implemented in pure Java and the same issue would arise.


    Using pcm.clone() is a safe solution, but it may not be the most efficient because it requires a new array to be instantiated for every frame.

    If you don’t need to decode the mp3 data in advance, then you should be able to design a mechanism which prevents GetPCM from being called until the queue is empty, and you can get by with one or two pre-allocated buffers.

    On the other hand, if you absolutely need to decode the mp3 data four seconds in advance, then you will inevitably need a lot of buffer space. But generally, performance is better if memory is allocated once at the beginning, and reused, and not repeatedly dynamically allocated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A pair is implemented as a templated struct. It provides… May 16, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer First don't use using when dealing with WCF proxy or… May 16, 2026 at 3:51 pm
  • Editorial Team
    Editorial Team added an answer I ended up writing a custom view and calling a… May 16, 2026 at 3:51 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

This is a subjective question just to get a general impression. As Java is
I'm particularly interested in Windows PowerShell , but here's a somewhat more general complaint:
I hope this question does not come off as broad as it may seem
I have a pretty general question here. What is the best + reliable way
I think this is not really possible but worth asking anyway. Say I have
It's too late to change the question, but more precise would have been to
I thought the C/C++ vs C#/Java performance question was well trodden, meaning that I'd
Seeing as Java doesn't have nullable types, nor does it have a TryParse(), how
Inspired by this question where there are differing views on SET NOCOUNT... Should we
Although I'm not new to programming in general, I am new to java, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.