Skip to content

Instantly share code, notes, and snippets.

@miguel-negrao
Last active June 21, 2017 09:23
Show Gist options
  • Save miguel-negrao/90792376dc7bc89cbd4b to your computer and use it in GitHub Desktop.
Save miguel-negrao/90792376dc7bc89cbd4b to your computer and use it in GitHub Desktop.
(
Udef(\binaural_encoder,{ |bus|
var angles = \angles.kr([0,0],0.1);
var in = UIn.ar(0,1);
var c = pi/180;
var encoder = FoaEncoderMatrix.newOmni;
var out = FoaEncode.ar( in , encoder );
var bufs, frames;
out = FoaTransform.ar(out, 'push', pi/2, angles[0]*c.neg, angles[1]*c);
out = UGlobalEQ.ar( out );
out = out * UEnv.kr( extraSilence: 0.2 );
Out.ar(bus + NumOutputBuses.ir + NumInputBuses.ir, out*UEnv.kr(useGlobalGain:0));
},
[[ \angles: UnitSpherical(0,0), UnitSphericalSpec()]])
.setSpec(\bus, PositiveIntegerSpec(0) );
Udef(\binaural_encoder_circle,{ |bus|
var angles = \angles.kr([0,0],0.1);
var in = UIn.ar(0,1);
var c = pi/180;
var encoder = FoaEncoderMatrix.newOmni;
var out = FoaEncode.ar( in , encoder );
var bufs, frames;
out = FoaTransform.ar(out, 'push', pi/2, LFSaw.ar(\f0.ukr(0,0,10)).range(0,2pi), \phi.ukr(0,-pi/2,pi/2));
out = UGlobalEQ.ar( out );
out = out * UEnv.kr( extraSilence: 0.2 );
Out.ar(bus + NumOutputBuses.ir + NumInputBuses.ir, out*UEnv.kr(useGlobalGain:0));
},
[[ \angles: UnitSpherical(0,0), UnitSphericalSpec()]])
.setSpec(\bus, PositiveIntegerSpec(0) );
Udef(\binaural_decoder,{ |bus|
//WXYZ signal
var in = In.ar(bus + NumOutputBuses.ir + NumInputBuses.ir,4);
var bufs = 4.collect{ |i| 2.collect{ |j|
"decoder_buf_%_%".format(i,j).asSymbol.ukr(0,BufSndFileSpec(1,512))
}};
var out = Mix.new(
4.collect({ arg i;
2.collect({ arg j;
Convolution2.ar(
in.at(i),
bufs.at(i).at(j),
framesize: 512 //for binaural we can hardcode to 512
)
})
})
);
Out.ar(0, out)
})
.setSpec(\bus,PositiveIntegerSpec(0));
~cpicBuffers = { |subjectID=1|
["W","X","Y","Z"].collect{ |letter,i| 2.collect{ |j|
var path = "%/FOA/decoders/cipic/44100/512/%/HRIR_%.wav".format(Atk.userKernelDir,subjectID.asString.padLeft(4, "0"), letter);
["decoder_buf_%_%".format(i,j).asSymbol, BufSndFile(path, useChannels:[j]) ]
}}.flat
})
~cpicBuffers.(27)
ULib.startup
//UBus needs this to work
//UBus is part of ImmLib
(
s.audioBusAllocator.reserve(s.options.firstPrivateBus, 64, true);
s.controlBusAllocator.reserve(0, 64, true);
)
//play white noise through encoder and then decode to binaural.
(
UScore(
UChain(\whiteNoise, [\binaural_encoder, [\bus, UBus.audio(\binaural,4) ]]).ugroup_(\bin),
UChain([\binaural_decoder, ~cpicBuffers.(27)++[\bus, UBus.audio(\binaural,4) ]]).ugroup_(\bin).addAction_(\addToTail)
).prepareAndStart.gui
)
//encode multchannel file into binaural
(
var p = ... //path
var angles = ... // angles like [[azi,elev],...] in degrees
var x = UChain(
*([[\diskSoundFile, [\soundFile, DiskSndFile(p) ]]]++
angles.collect{ |xs,i|
[\binaural_encoder, [\bus, UBus.audio(\binaural,4), \u_i_ar_0_bus, i, \angles, xs.asUnitSpherical ]]
})
).ugroup_(\bin).useSndFileDur;
UScore(
x,
UChain(0,1,x.dur+10,[\binaural_decoder, ~cpicBuffers.(20)++[\bus, UBus.audio(\binaural,4) ]]).ugroup_(\bin).addAction_(\addToTail)
).gui
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment