8 Replies - 607 Views - Last Post: 31 December 2017 - 10:53 AM Rate Topic: -----

#1 samp17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-December 17

Multichannel Audio into buffer with separate arrays

Posted 31 December 2017 - 05:13 AM

I am relatively new to programming and have been carefully studying the documentation regarding Java audio. My end goal is to create an audio analysis app however I am having issues regarding dealing with the incoming datastream.


I have requested a targetdataline with a specified format (For now assume I am dealing with just two channels, each with different content). What I need to do is split this stream into multiple circular buffers (one for each channel) that I can then process individually.

try {
	    line = (TargetDataLine) AudioSystem.getLine(info);
	    line.open(format);
	    
	    ByteArrayOutputStream out  = new ByteArrayOutputStream();
		int numBytesRead;
		byte[] data = new byte[line.getBufferSize() / 5];

		
		line.start();

		int size = 0;
		
		while (size< 1000) {
		  
			size++;
			
		   numBytesRead =  line.read(data, 0, data.length);
		 
		   out.write(data, 0, numBytesRead);
		}  
		
		 out.writeTo(outputStream);
		
		
		System.out.println(out.size());
	} catch (LineUnavailableException ex) {
	    // Handle the error ... 
		
		
	}finally{
		outputStream.close();
	}




This is my current code for getting the targetdataline into a buffer but this does not deal with splitting the channels. I have spent ages on google and cannot find a method for splitting the stream every two bytes. If anyone can even point me towards some reading material on this it would be much appreciated, or suggest a way to go about this.

Also, regarding this, is it more efficient to split the stream into multiple buffers (I could do each channel on a separate thread), or to write the entire stream to a buffer and then split the buffer content into separate channel data. (My head is telling me this is not efficient and I would need a bigger buffer size on one thread for this.

Thanks for your help. This is my first post so I hope I have done it right.

Is This A Good Question/Topic? 0
  • +

Replies To: Multichannel Audio into buffer with separate arrays

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,981
  • Joined: 20-September 08

Re: Multichannel Audio into buffer with separate arrays

Posted 31 December 2017 - 06:11 AM

You would need to know how the data are encoded in the stream. Do you know that?
Was This Post Helpful? 0
  • +
  • -

#3 samp17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-December 17

Re: Multichannel Audio into buffer with separate arrays

Posted 31 December 2017 - 06:21 AM

I believe I do. As I have requested the format I can determine I have unsigned 16-bit two channel stream running at 48kHz which is big endian.

It is my understanding that the stream iterates through each channel for each sample. therefore I would like to write two bytes to one buffer, and then the next two bytes to another buffer and repeat this process.

The only thing I do not know, is if there is a separation byte or any other data which may throw this method off. (If there's not it should be as straight forward as splitting the stream as described, I just do not know how).

I hope I am going down the right path with this, as there isnt too much info on dealing with channels of a multichannel device.
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,981
  • Joined: 20-September 08

Re: Multichannel Audio into buffer with separate arrays

Posted 31 December 2017 - 07:26 AM

Quote

It is my understanding that the stream iterates through each channel for each sample.

Yes, but that's vague. How do you know for certain how to isolate one channel from the other?
Was This Post Helpful? 0
  • +
  • -

#5 samp17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-December 17

Re: Multichannel Audio into buffer with separate arrays

Posted 31 December 2017 - 07:35 AM

I do not know for certain. This is just what I have found online that points towards this practice. It could be completely wrong, and if I am, I apologise for starting this thread incorrectly.
However it still does not address how I would go about getting a specific channel of audio from a multi channel audio interface. It is not for a specific audio interface, I would like this to work with different interfaces, so it was my understanding that requesting the targetdataline was the best way about doing this, as I could open with a specific format that the interface can handle and deal with the incoming stream.

There is no clear documentation or even spoken practice about how to do this, and yet it is a very common thing to want to do. Everything I have found online either describes a mono channel stream, or a stereo stream being recorded directly to a wav file.

If you have even any hints that would point me in the correct direction, I would appreciate it.
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,981
  • Joined: 20-September 08

Re: Multichannel Audio into buffer with separate arrays

Posted 31 December 2017 - 08:51 AM

Quote

I do not know for certain. This is just what I have found online that points towards this practice. It could be completely wrong, and if I am, I apologise for starting this thread incorrectly.

Well of course, you'd need to be certain, before you can write any code

Quote

However it still does not address how I would go about getting a specific channel of audio from a multi channel audio interface.

I don't understand the question (unless it's the same question). What do you mean? That Java api gives you a stream. What interface are you referring to?

This post has been edited by g00se: 31 December 2017 - 08:59 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#7 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,981
  • Joined: 20-September 08

Re: Multichannel Audio into buffer with separate arrays

Posted 31 December 2017 - 09:35 AM

The following might be of use
http://www.labbookpa...vaWavFiles.html
Was This Post Helpful? 0
  • +
  • -

#8 samp17   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-December 17

Re: Multichannel Audio into buffer with separate arrays

Posted 31 December 2017 - 09:46 AM

Maybe I haven't been clear in the question. It is still the same question (or at least the end result).

I have an 8 channel USB audio interface. I want to get the datastream of specific channels of this interface in separate byte arrays so that I can later analyse the data.

I can use the java API to detect the USB audio interface and to get a data stream that contains all of the channels on the interface. The question is how do I get specific channels of an interface? (or to separate the stream of all channels into specific channels). On OSX the java interfaces with core audio so theres nothing extra to do to get the stream, on windows I need the jasiohost framework to get the data stream. I am running osx so I can get this data stream of all channels, but I am stuck at how to divide it into channels.

Loads of applications allow for external audio interfaces to be used for exactly the same purpose, but I cannot work out how to achieve this and currently all of the content I have found online does not help me.
Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3704
  • View blog
  • Posts: 16,981
  • Joined: 20-September 08

Re: Multichannel Audio into buffer with separate arrays

Posted 31 December 2017 - 10:53 AM

I'm not sure whether more than two channels are supported by Java:

https://docs.oracle....l#getChannels--

Indeed, if you look at ExampleHost.java in the jasiohost API, you'll see that they only concern themselves with getting two channels.
Without that API, it will be a question of de-interleaving of the stream:
https://stackoverflo...udio-look-like#

This post has been edited by g00se: 02 January 2018 - 05:22 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1