It is currently Fri Apr 26, 2024 1:49 pm


Merge/mix Front and Rear samples

Sampling pipe organs and turning them into something you can play in Hauptwerk.
  • Author
  • Message
Offline

GKruizenga

Member

  • Posts: 23
  • Joined: Tue Apr 22, 2003 10:08 am
  • Location: Houten, Netherlands

Merge/mix Front and Rear samples

PostSat Jun 09, 2012 1:02 pm

Hi all,

I don't know if I can ask these technical questions right here, but I will give it a try...

I need to merge WAV-files (front and rear) 32 bit float into one WAV-file. I already have written some Java-code to
handle WAV-files (e.g. updating pitch information in the files etc.) and now want to write the code to merge/mix front and rear files into one WAV-file.

On the internet I have found several sources which are proposing the following:

1) Read the data-chunk of the Front and Rear file and convert those values to 32-bit float. That means for every 4 bytes I convert it to a long (4 bytes)
and then convert value to : sample = (value / maxRange) - 1 (so we have values between -1.0 and 1.0) and maxrange = 2^(32 - 1).
2) I do the scaling for both Front and Rear and the mixed values = (scaled-front + scaled-rear) / 2.0.
3) Now I convert the mixed-scale value back to a long and put it in the data-chunk of the WAV-file.

However it is now working as expected, the mixed value is generating a bad signal with a lot of noise.

I have tried with Adobe Audition and Audacity to mix the Front and Rear and via inspecting the data-chunk of the mixed file from both of the application to see
how the mixing is really done, however also without success.

Some example code, to get an idea what I am doing:

...
float maxRange = (float) Math.pow(2, (bytesPerSample * 8) - 1); // for 32-bit -> 2147483648
for (int i = 0; i < buffer.length / bytesPerSample; i++)
{
long valueFront = getIntFromBuffer(buffer, offset, bytesPerSample);
long valueRear = getIntFromBuffer(dataRearChunk, offset + 8, bytesPerSample);

float samplef1 = ((float) valueFront) / maxRange - 1.0f;
float samplef2 = ((float) valueRear) / maxRange - 1.0f;
float mixed = (samplef1 + samplef2) / 2.0f;
mixed += 1.0f;
mixed *= maxRange;
long outputSample = (long) mixed;
putIntIntoBuffer(buffer, offset, bytesPerSample, outputSample);
offset += bytesPerSample;
}
...

Any feedback is welcome!

Regards,
Gerald
Offline
User avatar

ReinerS

Member

  • Posts: 869
  • Joined: Thu Oct 14, 2004 11:52 am
  • Location: Braunschweig, Germany

Re: Merge/mix Front and Rear samples

PostSat Jun 09, 2012 2:23 pm

Hello Gerald,

what is not clear from your post is the format of your input files. The wav-format allows 32bit float and 32bit integer, which one do you have? (It is coded in the format chunk: Format-Code "1" is integer, format-code "3" is IEEE-float. When you samples are already in float, you will need to read and write them as float in your Java program, otherwise you will get garbage. If your samples are in integer format, then I think your method should work (of course you will need to map the range again).

Regards
Reiner
Offline

GKruizenga

Member

  • Posts: 23
  • Joined: Tue Apr 22, 2003 10:08 am
  • Location: Houten, Netherlands

Re: Merge/mix Front and Rear samples

PostSat Jun 09, 2012 4:10 pm

Thanks you very much! This was indeed the mistake I made, I was using the int-approach, while the format was float (type=3). Changed the code and now it is working fine!

Please find the changed code below:

...
int offset = 0;
ByteBuffer bufFront = ByteBuffer.wrap(buffer);
ByteBuffer bufRear = ByteBuffer.wrap(dataRearChunk);
bufFront.order(ByteOrder.LITTLE_ENDIAN);
bufRear.order(ByteOrder.LITTLE_ENDIAN);
for (int i = 0; i < buffer.length / bytesPerSample; i++)
{
float samplef1 = bufFront.getFloat(offset);
float samplef2 = bufRear.getFloat(offset + 8);
float mixed = (samplef1 + samplef2) / 2.0f;
bufFront.putFloat(offset, mixed);
offset += bytesPerSample;
}
buffer = bufFront.array();
...

Return to Creating sample sets / recording organs

Who is online

Users browsing this forum: No registered users and 1 guest