Import List of Frequencies and Levels

An interesting concept.
I’ve used “Plot Spectrum” to analyse some piano notes, then used a modified form the code from the previous message to synthesize the piano tones, and finally applied the original amplitude envelope to synthesized tones. This is a rather crude form of synthesis, but the result is undeniably piano-like.

Here’s a brief recording of the original piano notes followed by the synthesised notes.

The difference between the code used to synthesize this and the previously posted code is that this version applies a random phase shift to each of the generated frequencies rather than each frequency starting at 0 degrees.

;; Synthesise sound from spectrun analysis

(setf data'(

; data goes here
; in the form of two numbers
; frequency (Hz) and amplitude (dB)
; for example
; 440 -12

))

(setf out (s-rest 1)) ;initialise output

;; function to generate random phase shift
(defun rand ()
  (* (rrandom) 360))

;; loop through each data pair
(dotimes (i (/ (length data) 2))
  (let ((phase (rand))
    (amp (db-to-linear(nth (+ 1 (* i 2))data)))
    (freq (nth (* i 2)data)))
  ;; add new sine to output
  (setf out (sim 
    out 
    (scale amp(hzosc freq *sine-table* phase))))))
    
out ; return output

This is the code that copies an amplitude envelope from the left channel of a stereo pair and applies it to the right channel.

;; Apply amplitude envelope from left channel to right channel
;; Caution - there is no checking for silence
;; (inverse of silence is infinite).

;; calculate envelopes at 20 Hz
(setq block (truncate (/ *sound-srate* 20)))
(setf ltops
    (snd-avg (aref s 0) block block op-peak))    
(setf rtops
    (snd-avg (aref s 1) block block op-peak))

    
(mult ltops ; left channel envelope
  (aref s 1) ; original right channel
  (s-exp (diff (s-log 1.0)(s-log rtops)))) ; inverse of right channel envelope