Main Page       Index


sam.lsp


 sam.lsp
 Version 1.00    31 October 2004
 Author  Steven Jones

 Contact jones57@swbell.net include the word "nyquist" in subject line

 The contents of this file are released under the terms of the GNU General
 Public License. See the file LICENSE.txt

 Functions for reading sample files


function

sam:read

 (sam:read filename [:time-offset][:dur][:nchans][:transpose][:base][:r])
 Read sound file

 filename   - string.  The fully qualified sound file name.

 :time-offset - flonum. Time offset in seconds from start of file. 
              Default 0
              NOTE: The time offset is applied *BEFORE* stretching.

 :dur       - flonum. The duration in seconds to be read.
              Default 10000
              NOTE: The duration is *BEFORE* stretching.

 :nchans    - integer. The number of channels to return.
              If nchans is 1, return a mono sound, stereo channels files are mixed
              If nchans is 2, return 2 channel array, mono signals are promoted
              For any other nchans value, return the sound as is. Default NIL

 :transpose - integer. Number of half-steps to transpose the sound. The
              transposition value is summed with the Nyquist transpose environment. The
              step size ratio may be changed for alternative tunings. See base argument
              below. Default 0

 :base      - flonum. The transposition step size ratio, Default +12ROOT2+

 :r         - flonum. A direct stretch factor. Default 1

 return     - SOUND or sound vector.


function

sam:loop

 (sam:loop filename hz [:tune][:sr][:loop-as-time][:start][:end][:fm][:dur][:phase][:refkey])
 Generate sound by extracting wave table from sound file

 filename - string.  

 hz       - flonum. The frequency in Hertz of the result.

 :tune    - flonum. An additional tuning factor may be used to nudge the pitch.

 :sr      - integer. The sample rate of the file. Default *sound-srate*

 :loop-as-time - bool. A flag which indicates how loop start/end points
            are interpreted. If true the loop position is set a time offset in seconds
            from start of the file. If false the loop points are treated as sample
            numbers. Default t

 :start   - flonum. Loop start point either as time or sample number.
            Default 0

 :end     - flonum. Loop end point either as time or sample number. 
            end > start, default end = start + 1 (second)

 :refkey  - flonum. Set reference pitch (as MIDI key number). If not
            specified the tones reference frequency is set implicitly from the loop
            duration. This is appropriate when the loop contains a single cycle. If
            however the loop contains multiple cycles of the fundamental it is
            necessary to set the reference pitch explicitly.  Default NIL

 :fm      - sound.  An FM modulation source. If specified the duration of the
            modulation sources determines the duration of the resulting tone. 
            Default (s-rest)

 :dur     - flonum. The tones duration, if and only if the FM option is not
            used. Default 1 second.

 :phase   - any. See phase function in utilities.lsp

 return   - sound.


View the Sourcecode :



;; sam.lsp
;; Version 1.00    31 October 2004
;; Author  Steven Jones
;;
;; Contact jones57@swbell.net include the word "nyquist" in subject line
;;
;; The contents of this file are released under the terms of the GNU General
;; Public License. See the file LICENSE.txt
;;
;; Functions for reading sample files
;; 

(require 'efx)
(provide 'sam)
(current-file "sam")


;; @doc function sam:read
;; (sam:read filename [:time-offset][:dur][:nchans][:transpose][:base][:r])
;; Read sound file
;;
;; filename   - string.  The fully qualified sound file name.
;;
;; :time-offset - flonum. Time offset in seconds from start of file. 
;;              Default 0
;;              NOTE: The time offset is applied *BEFORE* stretching.
;;
;; :dur       - flonum. The duration in seconds to be read.
;;              Default 10000
;;              NOTE: The duration is *BEFORE* stretching.
;;
;; :nchans    - integer. The number of channels to return.
;;              If nchans is 1, return a mono sound, stereo channels files are mixed
;;              If nchans is 2, return 2 channel array, mono signals are promoted
;;              For any other nchans value, return the sound as is. Default NIL
;;
;; :transpose - integer. Number of half-steps to transpose the sound. The
;;              transposition value is summed with the Nyquist transpose environment. The
;;              step size ratio may be changed for alternative tunings. See base argument
;;              below. Default 0
;;
;; :base      - flonum. The transposition step size ratio, Default +12ROOT2+
;;
;; :r         - flonum. A direct stretch factor. Default 1
;;
;; return     - SOUND or sound vector.
;;

(defun sam:read (filename &key time-offset dur nchans transpose base r)
  (let* ((sig-a (s-read filename 
		      :time-offset (or time-offset 0)
		      :dur (or dur 10000)
		      :nchans (or nchans 1)))
	 (transpose (or transpose 0))
	 (base (or base +12ROOT2+))
	 (r (or r 1))
	 (chans (second *rslt*))
	 (sig-b (efx:transpose sig-a :transpose transpose :base base :r r)))
    (cond ((null nchans) sig-b)
	  ((numberp nchans)
	   (cond ((= nchans 1)(->mono sig-b))
		 ((= nchans 2)(->asound sig-b))
		 (t sig-b)))
	  (t sig-b))))


;; @doc function sam:loop 
;; (sam:loop filename hz [:tune][:sr][:loop-as-time][:start][:end][:fm][:dur][:phase][:refkey])
;; Generate sound by extracting wave table from sound file
;;
;; filename - string.  
;;
;; hz       - flonum. The frequency in Hertz of the result.
;;
;; :tune    - flonum. An additional tuning factor may be used to nudge the pitch.
;;
;; :sr      - integer. The sample rate of the file. Default *sound-srate*
;;
;; :loop-as-time - bool. A flag which indicates how loop start/end points
;;            are interpreted. If true the loop position is set a time offset in seconds
;;            from start of the file. If false the loop points are treated as sample
;;            numbers. Default t
;;
;; :start   - flonum. Loop start point either as time or sample number.
;;            Default 0
;;
;; :end     - flonum. Loop end point either as time or sample number. 
;;            end > start, default end = start + 1 (second)
;;
;; :refkey  - flonum. Set reference pitch (as MIDI key number). If not
;;            specified the tones reference frequency is set implicitly from the loop
;;            duration. This is appropriate when the loop contains a single cycle. If
;;            however the loop contains multiple cycles of the fundamental it is
;;            necessary to set the reference pitch explicitly.  Default NIL
;;
;; :fm      - sound.  An FM modulation source. If specified the duration of the
;;            modulation sources determines the duration of the resulting tone. 
;;            Default (s-rest)
;;
;; :dur     - flonum. The tones duration, if and only if the FM option is not
;;            used. Default 1 second.
;;
;; :phase   - any. See phase function in utilities.lsp
;;
;; return   - sound.
;;

(defun sam:loop (filename hz &key (tune 1) sr (loop-as-time 't) start end refkey fm (dur 1)(phase 0))
  ;(here "sam:loop   hz = " hz   "    refkey = " refkey) ;;; DEBUG 
  (let* (loop1 loop2 loop-dur tab refhz)
	 (setf sr (float (or sr *sound-srate*)))
	 (setf start (or start 0))
	 (setf end (or end (+ start 1)))
	 (if loop-as-time
	     (setf loop1 start
		   loop2 end)
	   (setf loop1 (/ start sr)
		 loop2 (/ end sr)))
	 (setf loop-dur (- loop2 loop1))
	 (setf refhz (if refkey 
			 (step-to-hz refkey)
		       (/ loop-dur)))
	 ;(here "sam:loop    refkey = " refkey "    refhz = " refhz) ;;; DEBUG    
	 (setf fm (or fm (s-rest dur)))
	 (setf tab (list (s-read filename :time-offset loop1 :dur loop-dur :nchans 1)
		    (hz-to-step refhz) t))
    (fmosc (hz-to-step (* hz tune)) fm tab (phase phase))))


Main Page       Index