Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (773)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Comparing binaural methods  (Read 22173 times)
0 Members and 1 Guest are viewing this topic.
Offline philfrei
« Posted 2016-02-25 23:38:29 »

Not too long ago, we were having a discussion about binaural sound effects, and Riven wrote about the technique of introducing a time difference between the left and right tracks to create a pan effect.
http://www.java-gaming.org/topics/is-a-binaural-sound-engine-in-java-possible/36982/msg/352258/view.html#msg352258

I wanted to investigate this further, so I wrote a program to compare two ways of creating stereo:
1) by having a mono signal be given different volumes in each ear
2) by having a mono signal be given with a time difference in each ear



Jar file, with source code is here


I put in a couple different source sounds:
1) sine wave, with ability to specify the Hertz and the length of time in seconds (can test specific ranges--supposedly the time effect works best with low notes and the volume effect works best with high notes)
2) sine sweep, from low to high (about 4 seconds long)
3) frog sample (recorded from a local creek, saved as a mono array of PCM floats in the range -1 to 1)

There are two sliders:
1) volume panning, uses the following formula to convert the slider value ("volumePan") to channel factors:
1  
2  
3  
4  
5  
6  
7  
      float pan = (volumePan + 1) / 2;
     
       for (int i = 0; i < size; i++)
      {
         stereoPcmSample[i * 2] = pcmSample[i] * (1 - pan);
         stereoPcmSample[i * 2 + 1] = pcmSample[i] * pan;
      }

2) sound frame differences, which can range from -128 to 128  (if negative, left channel leads by given number of frames, if positive, right channel leads by given number of frames).

For reference, a sound frame here is 1/44100th of a second, or approximately 0.000_023 seconds in length.

Another reference, the distance sound can typically travel in air is 343.59 meters per second. So, in one frame, we have 343.59/44100 equal to approximately 0.78 centimeters.

Thus, the distance from one ear to another should consume, what? (jokes about fat heads not needed)
I get about 30 cm if I roll a ruler around my head. What does that come to? 36 frames for the biggest time difference between the two ears?

In fact, when I was listening to this with headphones, I was putting the left-most or right-most setting of the framediff slider at between 32 to 36 (or -32 to -36). So, I am pleased this calculation seems to come out. Making the difference larger than that didn't seem to increase the location towards the sides.

I found the "frog croak" to be the clearest example. With the sweeping tone, it seems like the location may move around a bit as a function of the pitch with the time-difference method. Not sure how to account for this.

Is it my imagination or wishful thinking that putting the frog at a location to the side, and matching it as best as possible with the volume-pan, that the time-difference method sounds a bit cleaner somehow?

Feedback much desired. I made the code available in the jar so folks could verify what is being presented. As always, as a musician who taught himself Java, any suggestions on the coding itself are also appreciated (either on this thread or as personal messages).

music and music apps: http://adonax.com
Offline Catharsis

JGO Ninja


Medals: 75
Projects: 1
Exp: 21 years


TyphonRT rocks!


« Reply #1 - Posted 2016-02-26 00:29:24 »

I'd say this is just amplitude panning; not actually binaural yet. You're more likely to end up with something that sounds like a ping-pong or tremelo effect if you alternated things quick enough with what you are doing. Perhaps try a square or saw wave if you want things to stand out a bit more.

Actual binaural audio refers to applying a HRTF (head related transfer function) to filter audio frequencies which alters more than just the onset of a sound event between each ear. From there you can also apply the inverse square law for simulated distance.

Check out the TyphonRT Video Suite:
http://www.typhonvideo.com/

Founder & Principal Architect; TyphonRT, Inc.
http://www.typhonrt.org/
http://www.egrsoftware.com/
https://plus.google.com/u/0/+MichaelLeahy/
Offline Catharsis

JGO Ninja


Medals: 75
Projects: 1
Exp: 21 years


TyphonRT rocks!


« Reply #2 - Posted 2016-02-26 00:48:00 »

I'd say to continue your investigation look into SuperCollider ugen / DSP code:

general links:
https://audioprogramming101.wordpress.com/2014/03/04/create-binaural-soundscapes-using-supercollider-and-hrtfs/
https://ccrma.stanford.edu/workshops/gaffta2010/spatialsound/

Looks like the last one might have a bunch of video from the workshop available.

Source code for binaural / hrtf loading which uses the Convolution2 ugen of SC:
https://github.com/supercollider-quarks/AmbIEM/blob/master/Classes/Binaural.sc

Convolution ugens from SC:
https://github.com/supercollider/supercollider/blob/master/server/plugins/Convolution.cpp

Stepping through both the Binaural.sc for loading the HRTF and then stepping through Convolution.cpp  will get you on the right track to implementing something yourself.

Check out the TyphonRT Video Suite:
http://www.typhonvideo.com/

Founder & Principal Architect; TyphonRT, Inc.
http://www.typhonrt.org/
http://www.egrsoftware.com/
https://plus.google.com/u/0/+MichaelLeahy/
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2016-02-26 05:56:27 »

Quote
I'd say this is just amplitude panning; not actually binaural yet. You're more likely to end up with something that sounds like a ping-pong or tremelo effect if you alternated things quick enough with what you are doing. Perhaps try a square or saw wave if you want things to stand out a bit more.

Actual binaural audio refers to applying a HRTF (head related transfer function) to filter audio frequencies which alters more than just the onset of a sound event between each ear. From there you can also apply the inverse square law for simulated distance.

@Catharsis
Thanks for the links in the second post. I will look into this further. I can see the point that "binaural" might more precisely refer to a combination of timing and filtering, to take into account the audio shadow created by the head itself as well as the timing differences. In any event, I just specifically wanted to hear for myself if adding a timing offset would also contribute to the panning effect, and what it would sound like.

The demo I wrote doesn't do anything fancy with the amplitude/volume panning. The math is very simplistic. To make this into a 3D system, yes, the proper scalings would have to be employed, as well as using trig for the corresponding angles, and some sort of low pass filter for the unequal timbral degradation of sound over distance.

But the second slider, and "frame diff" play button do nothing except change the onset times. Whatever it is, it is not amplitude panning. The amplitudes are identical. You can see this by dumping the DSP data (right button). The left and right data streams are identical, just offset by frame numbers.

I'm not sure I understand your point about ping-ponging. That won't occur until the two sounds no longer fuse perceptually. At 30 frames or under, the audio event seems to me to be heard as a single event, still. If you go over that, then some weirdness would start to happen (such as ping pong). The scale of the second slider is far too large for the range where the effect is most relevant. But again, I was doing this to hear for myself, not rely on what theory says one should hear. and so wanted to test larger ranges as well and hear how it degrades or not.

music and music apps: http://adonax.com
Offline philfrei
« Reply #4 - Posted 2016-03-16 01:58:30 »

Just posted a redo of the above jar. Here it is again. I meant to do more with it, but only got the following done: a bit more info about the "distance" and time implied by the difference in frames for the second panning method. Also, got a standard sort of calculation relating values to db's. But the pan that uses volumes could still be improved. In particular, the energy at the ends is greater than the perceived volume in the middle. Something like a sine function that has the middle be 0.7 + 0.7 (left and right) instead of 0.5 + 0.5, it turns out, sounds better.


music and music apps: http://adonax.com
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1861 views)
2018-06-10 19:43:48

EgonOlsen (1883 views)
2018-06-10 19:43:44

EgonOlsen (1252 views)
2018-06-10 19:43:20

DesertCoockie (1684 views)
2018-05-13 18:23:11

nelsongames (1350 views)
2018-04-24 18:15:36

nelsongames (1968 views)
2018-04-24 18:14:32

ivj94 (2739 views)
2018-03-24 14:47:39

ivj94 (1942 views)
2018-03-24 14:46:31

ivj94 (3032 views)
2018-03-24 14:43:53

Solater (1083 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!