AudioBuffer. Old dog - New tricks

54 views
Skip to first unread message

George

unread,
May 22, 2022, 7:03:33 PM5/22/22
to Extempore
AudioBuffer. Old dog - New tricks

In my previous post I spoke of my struggle to figure out how AudioBuffer works.
I have since spent some time experimenting with various functions
in the "audiobuffer.xtm" and have made some progress.
I have attached a file "piano_3_notes_in_assets.xtm" in which I do some experiments
with audiobuffers, attempting to:
;; Load audio buffers
;; Print buffer component values
;; Play buffers
;; Play repeatedly
;; Externally from the dsp, change the note played.
;; Concatenate two buffers.
;; Concatenate three buffers.
;; Mix two buffers to sound together.
;; and
;; Using a different file from assets:
;;     Some experiments with AudioBuffer_read_interp
;;     and
;;     AudioBuffer_read_interp_pw

I have attached the file "piano_3_notes_in_assets.xtm" below.

As an example I use  AudioBuffer_read_interp_mix  from the core library. See:
https://github.com/digego/extempore/blob/81b1193dac2dd60f4cb51999c85a7cb9ce1f2bf0/libs/core/audiobuffer.xtm#L400

(sys:load "libs/core/audiobuffer.xtm")

;; Create audio buffers from three sample files in
;;  extempore/assets/samples/salamander-piano/ff/

(bind-val pianoC6 AudioBuffer* (AudioBuffer "assets/samples/salamander-piano/ff/C6-ff.wav"))
(bind-val pianoDsh6 AudioBuffer* (AudioBuffer "assets/samples/salamander-piano/ff/D#6-ff.wav"))
(bind-val pianoFsh6 AudioBuffer* (AudioBuffer "assets/samples/salamander-piano/ff/F#6-ff.wav"))

;; AudioBuffer_read_interp_mix
;; Mixing two buffers to sound together.
;; This version mixes the second buffer, ab2 in with the first buffer ab1
;; In the final line changing the midi 60 frequency from 264.3 Hz will shift the output frequency.
;; Changing the 0.5 value alters the mix ratio of ab1/ab2
;; I found it is important to reset the phase to zero on each re-evaluation.

(bind-func dsp:DSP
  (let ((ab1 pianoC6) ;; pianoFsh6 pianoC6 pianoDsh6
        (ab2 pianoFsh6))
    (AudioBuffer_set_phase ab1 0.)  ;; set the phase of ab1 to zero !!
    (lambda (in time chan dat)
      (AudioBuffer_read_interp_mix ab1 ab2 (* 1.3 264.3) chan 0.5)))) ;; altering the mix value (a float) emphasises one or other buffer

(dsp:set! dsp)

;; Without phase reset the output will play the first time and only once!
;; To show this, comment out the phase line and print the ab1 components

($ (println pianoC6)).
;; Notice the PHASE number for the ab1 buffer is non zero.
;; Need to set_phase to zero on each re-evaluation.
;; Not sure if the non-zero phase was intentional. ??
;; It took me a long time to figure out why the thing would run only once despite re-evaluations.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; I have a copy of the "piano_3_notes_in_assets.xtm" file here:
https://gist.github.com/georgejwright/3e2442fc9231a6d30e42f9eb75ea4793


George

unread,
May 31, 2022, 6:15:36 PM5/31/22
to Extempore

AudioBuffer - even more tricks

Gidday again 

I have loaded another file of further developed examples of use of AudioBuffer onto my Gist.

“play_5_notes_in_temp.xtm”

To run all the examples you require three audio files loaded into extempore/tmp/

I attach the files in the following post. “five_notes.wav" , “WellMayWeSay75.wav”, “nothingWillSave.wav"

I copy here  a small section from “play_5_notes_in_temp.xtm”

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(sys:load "libs/core/audiobuffer.xtm")

;; This dsp version will load the "five_notes.wav" file from a tmp folder into a buffer.

(bind-func dsp:DSP

  (let ((ab (AudioBuffer "tmp/five_notes.wav")))

    (AudioBuffer_set_playhead ab 0)

    (lambda (in time chan dat)

      (AudioBuffer_read ab chan))))


;; and play the buffer


(dsp:set! dsp)


;; Now if I play the same file in the Audacity app with the “Start & End Selections”

;; in the selection toolbar set to Samples, I find that the first note begins at zero

;; and ends after about 26867 samples. The next silence position  is at about 53250 samples.

;; I obtained the boundaries between notes in Audacity by looking for silence points in the file.

:; The sample note boundaries in "five_notes.wav" are (0 26867 53250 79702 106084 132675)


;; Generate a buffer just once instead of regenerating repeatedly.


(bind-val ab5 AudioBuffer* (AudioBuffer "tmp/five_notes.wav"))

($ (println ab5))


;; Now generate an array of note boundaries


(bind-func smpl_bndry ;; boundaries in an array

  (lambda ()

    (let ((array6:|6,i64|* (alloc)))            ;; An array of 6 i64 items

      (afill! array6 0 26867 53250 79702 106084 132675)  ;; Fill array

      array6)))


($ (println (smpl_bndry)))


;; AudioBuffer_read_interp_pw

;; Play a selection of notes from a random starting point.


(bind-func dsp:DSP

  (let ((ab ab5)   ;; the audiobuffer already built

        (v6 (smpl_bndry))    ;; The array of six i64 integers already built

        (note_r (imp_rand1_i64 5) ))   ;; A random i64 from 0 1 2 3 4

    (AudioBuffer_set_loop_start ab (aref v6 note_r)) ;; start from random silent position

    (AudioBuffer_set_phase ab5 0.0)  ;; I found this step to be critical for some functions

    ;; set the loop frames large enough to include 2 notes.

    (AudioBuffer_set_loop_frames ab (* 2 26867)) ;; This should select just 32notes

    (lambda (in time chan dat)

      (AudioBuffer_read_interp_pw ab (* 0.9 261.6) chan 0.4)))) ;; There's a mix going on between neighbouring segments


;; changing the root pitch causes notes to be played at higher/lower frequency.

;; changing the pw value 0.0 to 1.0 will shift emphasis in the mix.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


I still haven’t figured out how to use the “frames” version of AudioBuffer_read:

https://github.com/digego/extempore/blob/81b1193dac2dd60f4cb51999c85a7cb9ce1f2bf0/libs/core/audiobuffer.xtm#L271

or even what it is supposed to do, so if anyone could enlighten me I would appreciate it.


Regards

George

George Wright

unread,
May 31, 2022, 6:28:13 PM5/31/22
to extemp...@googlegroups.com
File is on my Gist here:

Three audio files for “play_5_notes_in_temp.xtm”


five_notes.wav
WellMayWeSay75.wav
nothingWillSave.wav
Reply all
Reply to author
Forward
0 new messages