Walking with Bats (+ Csound Code)

Greetings Csounders,

I’ve just released an album of sound recordings called Walking with Bats:

It’s basically a collection of narrated wildlife walks: me plus a handheld ultrasound detector, conveying my experiences in the field as I record echolocations from UK and Irish bat species.

There are some bonus tracks at the end, including a Csound-based, binaural reconstruction of the foraging activity of a common pipistrelle bat, and stereo noctule bat vocalisations, rendered using a non-realtime heterodyne bat detector (Csound again). See tracks 10 and 11, respectively.

Thought this might interest people. Code below (I’m sure there are many better ways to do this, but it worked for me).

Best,
Mark F.

; Binaural bat spatialisation.
<CsoundSynthesizer>

<CsOptions>
-odac
;-o test.wav
</CsOptions>

<CsInstruments>

sr        =         96000 ; Must match sample rate of source.
ksmps     =         10
nchnls    =         2
0dbfs     =         1

seed 1

////////////////////////////////////////////////////////////////////////////////////////////

instr BinBats

idur      =         p3
ifile     =         p4
iamp      =         1
ipitch    =         1
iskip     =         0
iwrap     =         0

; HRTF initialisation parameters.
ioverlaps =         16       ; Overlaps for STFT processing.
iradius   =         9        ; Head radius.
iminaz    =         -70
imaxaz    =         70
iminel    =         60
imaxel    =         90

//////////////////

; Gentle interpolated az/el (typically offers best results).
kazjit    jitter    0.5, 1, 3
;kazjit    =         kazjit + 3
kaz       randomi   iminaz, imaxaz, 2 + kazjit

keljit    jitter    0.5, 1, 3
;keljit    =         keljit + 1
kel       randomi   iminel, imaxel, 2 + keljit

; Override az/el randomisation (constant position).
;kaz       =         30
;kel       =         20

//////////////////

ilen      filelen             ifile
          print               ilen

asig      diskin2   ifile, ipitch, iskip, iwrap
amix      =         asig * iamp

aL, aR    hrtfmove2 amix, kaz, kel, "hrtf-96000-left.dat", "hrtf-96000-right.dat", ioverlaps, iradius, sr

outs      aL, aR

endin

</CsInstruments>

<CsScore>

i"BinBats" 0 1 "source.wav"

</CsScore>

</CsoundSynthesizer>


; Non-realtime Heterodyne Bat Detector.
<CsoundSynthesizer>

<CsInstruments>

sr        =         192000
ksmps     =         10
nchnls    =         2
0dbfs     =         1

/*
Set channel processing parameters.
1 = Mono or stereo input, mono output (stereo channel isolated). [nchnls = 1]
2 = Stereo input, stereo output ('straight' stereo). [nchnls = 2]
*/
giflag    =         2

giSine    ftgen     0, 0, 16384, 10, 1

////////////////////////////////////////////////////////////////////////////////////////////

instr FStoHET

idur      =         p3
ifilecod  =         p4
igain     =         2
iskip     =         0
iwrap     =         1

; Heterodyne oscillator & filter settings.
iHETtuning          =         23

ituning   =         iHETtuning * 1000
icut      =         ituning * 0.75
ibw	=	50

aosc      poscil    igain, ituning, giSine

//////////////////

ichn	filenchnls	ifilecod
ilen	filelen		ifilecod
prints	"\nSet idur to %f.\n\n", ilen

; Mono input, mono output (nchnls = 1).
if (ichn == 1 && giflag == 1) then

          puts "\nMono input, mono output. [nchnls = 1]\n", 1

          arec      diskin2   ifilecod, 1, iskip, iwrap
          asig      =         arec * aosc

	; Remove tuned oscillator signal.
          abrf1     butterbr  asig, ituning, ibw
          abrf2     butterbr  abrf1, ituning, ibw

	; Remove unwanted HF material.
          alpf1     butterlp  abrf2, icut
          alpf2     butterlp  alpf1, icut
          alpf3     butterlp  alpf2, icut
          alpf4     butterlp  alpf3, icut
          alpf5     butterlp  alpf4, icut

	; Output.
	out       alpf5

; Stereo input, mono output (nchnls = 1). Isolates stereo channel.
elseif (ichn == 2 && giflag == 1) then

          puts "\nStereo input, mono output (isolated channel).\n[nchnls = 1]\n", 1

          arecL, arecR        diskin2   ifilecod, 1, iskip, iwrap

          ; Isolate left or right channel.
          asig      =         arecL * aosc
          ;asig      =         arecR * aosc

          abrf1     butterbr  asig, ituning, ibw
          abrf2     butterbr  abrf1, ituning, ibw

          alpf1     butterlp  abrf2, icut
          alpf2     butterlp  alpf1, icut
          alpf3     butterlp  alpf2, icut
          alpf4     butterlp  alpf3, icut
          alpf5     butterlp  alpf4, icut

          ; Output.
          out       alpf5

; Stereo input, 'straight' stereo output (nchnls = 2).
elseif (ichn == 2 && giflag == 2) then

          puts "\nStereo input, stereo output ('straight' stereo). [nchnls = 2]\n", 1

          arecL, arecR        diskin2   ifilecod, 1, iskip, iwrap

          asigL     =         arecL * aosc
          asigR     =         arecR * aosc

          ; Left channel filtering.
          abrfL1    butterbr  asigL, ituning, ibw
          abrfL2    butterbr  abrfL1, ituning, ibw

          alpfL1    butterlp  abrfL2, icut
          alpfL2    butterlp  alpfL1, icut
          alpfL3    butterlp  alpfL2, icut
          alpfL4    butterlp  alpfL3, icut
          alpfL5    butterlp  alpfL4, icut

          ; Right channel filtering.
          abrfR1    butterbr  asigR, ituning, ibw
          abrfR2    butterbr  abrfR1, ituning, ibw

          alpfR1    butterlp  abrfR2, icut
          alpfR2    butterlp  alpfR1, icut
          alpfR3    butterlp  alpfR2, icut
          alpfR4    butterlp  alpfR3, icut
          alpfR5    butterlp  alpfR4, icut

          ; Output.
          outs       alpfL5, alpfR5

else

          puts      "\nERROR:\nChannel configuration not recognised.\nCheck input channels, file location/name & flag value.\n------|\n", 1

endif

endin

</CsInstruments>

<CsScore>

i"FStoHET" 0 1 "source.wav"

</CsScore>

</CsoundSynthesizer>












Thanks for sharing Mark. Really interesting :slight_smile:

1 Like

Very nice!
Thanks for sharing the code!
Tarmo

N, 30. november 2023 14:56 Rory Walsh via The Csound Community <noreply@forum.csound.com> kirjutas:

1 Like