No subject



;;;
;;;
;;; File:         csound-example.cm
;;;
;;; Summary:      Example for using Stella with the :CSOUND syntax
;;;
;;; Author:       Tobias Kunze
;;; e-Mail:       t@kunze.stanford.edu
;;; Org:          CCRMA, Stanford University
;;;
;;; orig-Date:    14-Sep-96 at 23:28:08
;;; Last-Mod:     12-Jan-97 at 18:04:01 by Tobias Kunze
;;;
;;; Revision:     1.1
;;;
;;; Description:  Edit the variables in the section marked *** VARIABLES ***
;;;               below and load the file.
;;;
;;; Changes:
;;;   12-Jan-97   tk   added Stella import



(let (;; 
      ;; *** BEGIN VARIABLES ***
      ;;
      (orc-file "/tmp/csound-ex.orc")
      (sco-file "/tmp/csound-ex.sco")
      (snd-file "dac")			; "dac" generates sound in real-time
      ;; 
      ;; *** END VARIABLES ***
      ;;
      )


;;;
;;; 1) Create a CSound orchestra file.

  (with-open-file (orc orc-file
		       :direction :output
		       :if-does-not-exist :create
		       :if-exists :supersede)
      
    (format orc "sr = 44100~%~
                   kr = 4410~%~
                   ksmps = 10~%~
                   nchnls = 1~%~
                   ~%~
                   ;;;~%~
                   ;;; simple oscillator with envelope~%~
                   ;;;~%~
                   ;;; f1: 0  <size>  10  1 0 1 0 1 0 1 0 1 0 1~%~
                   ~%~
                   instr 1~%~
                     idur = p3~%~
                     iamp = p4*32767       ; scale logical amplitude~%~
                     ifreq = p5~%~
                     iattackt = p6~%~
                     irelt	= p7~%~
                   ~%~
                     k1	linen	iamp, iattackt, idur, irelt~%~
                     a1	oscili	k1, ifreq, 1~%~
                   	out	a1~%~
                   endin~%"))

;;;
;;; 2) Create a Stella output stream.  This stream holds all additional 
;;;    information needed by CSound to process the generated score file.

  (open-cmd (format nil "~a ~{~a ~s~}"
		    sco-file
		    (list 'args "-A -d -m0"
			  'header "f1 0 8192  10  1 0 1 0 1 0 1 0 1 0 1"
			  'orchestra orc-file
			  'output snd-file
			  'play t)))

#| 
-----------------------------------------------------------------------------
EXAMPLE STELLA INTERACTION
-----------------------------------------------------------------------------

Stella [Top-Level]: open "/tmp/csound-ex.sco" args "-A -d -m0" output
  "dac" header "f1 0 8192  10  1 0 1 0 1 0 1 0 1 0 1" orchestra
  "/tmp/csound-ex.orc" 
Stream: #<File: "/tmp/csound-ex.sco">
Stella [Top-Level]: list io-streams
Io-Streams:
     1. #<File: "/tmp/csound-ex.sco"> 
Stella [Top-Level]: show io-streams[1]

Object:    #<File: "/tmp/csound-ex.sco">
Type:     Csound-Score-File
Status:   Normal
Position: 1 in Io-Streams
Slots:
          Header       "f1 0 8192  10  1 0 1 0 1 0 1 0 1 0 1"
          Container    #<CONTAINER: Io-Streams>
          Flags        0
          Finalizer    unset
          Initializer  unset
          Clock        unset
          Scheduling   :RUNTIME
          Direction    :OUTPUT
          Timescale    unset
          End          unset
          Start        unset
          Stream       unset
          Element-Type CHARACTER
          Play         :ASK
          Name         "/tmp/csound-ex.sco"
          Args         " -A -d -m0"
          Syntax       #<Syntax: CSOUND>
          Output       "dac"
          Orchestra    "/tmp/csound-ex.orc"
Stella [Top-Level]: edit io-streams[1]
Editing #<File: "/tmp/csound-ex.sco">
Type ? for help.
Edit: play
The value of PLAY is :ASK
new value: (<cr>=Unchanged)  T
The value of PLAY is T
Edit: q
Stella [Top-Level]: 
-----------------------------------------------------------------------------
|#



;;;
;;; 3) Define an instrument class. 

(defobject beep (csound-note)
  ((ins :initform 'i1) duration amplitude pitch
   (attack :initform .05) (release :initform .25))
  (:parameters ins time duration amplitude pitch attack release))


;;;
;;; 4) Create musical structure.

(algorithm ninth beep (amplitude .5)
  (setf pitch (- (item (pitches e e f g g f e d c c d e e d d) :kill t) 50)
	rhythm (item (rhythms (rhythms q for 12 tempo 160) q. e h tempo 160))
	duration rhythm))

#|
-----------------------------------------------------------------------------
EXAMPLE STELLA INTERACTION
-----------------------------------------------------------------------------

Stella [Top-Level]: new thread
Name for new Thread: (<cr>=Thread-10) ninth
New object position: (<cr>=Top-Level) 
Stella [Top-Level]: new beep
Number of beeps to create: (<cr>=*) 
Slots and values: {pitch (- (pitches e e f g g f e d c c d e e d d) 50)
  rhythm (rhythms (rhythms q for 12 tempo 160) q. e h tempo 160) 
  duration $rhythm amplitude .5}
New object position: (<cr>=Pasteboard) ninth
Stella [Top-Level]: tlist ninth
Start time offset:(<cr>=None) 
Ninth
    0.00    1. #<i1 1.0 1.0 0.25 829.62756 0.1 0.5>
    1.00    2. #<i1 1.0 1.0 0.25 829.62756 0.1 0.5>
    2.00    3. #<i1 1.0 1.0 0.25 849.2283 0.1 0.5>
    3.00    4. #<i1 1.0 1.0 0.25 891.99554 0.1 0.5>
    4.00    5. #<i1 1.0 1.0 0.25 891.99554 0.1 0.5>
    5.00    6. #<i1 1.0 1.0 0.25 849.2283 0.1 0.5>
    6.00    7. #<i1 1.0 1.0 0.25 829.62756 0.1 0.5>
    7.00    8. #<i1 1.0 1.0 0.25 793.6648 0.1 0.5>
    8.00    9. #<i1 1.0 1.0 0.25 761.62555 0.1 0.5>
    9.00   10. #<i1 1.0 1.0 0.25 761.62555 0.1 0.5>
   10.00   11. #<i1 1.0 1.0 0.25 793.6648 0.1 0.5>
   11.00   12. #<i1 1.0 1.0 0.25 829.62756 0.1 0.5>
   12.00   13. #<i1 1.5 1.5 0.25 829.62756 0.1 0.5>
   13.50   14. #<i1 0.5 0.5 0.25 793.6648 0.1 0.5>
   14.00   15. #<i1 2.0 2.0 0.25 793.6648 0.1 0.5>
Stella [Top-Level]: 
-----------------------------------------------------------------------------
|#


;;;
;;; 5) Generate output.

(mix-cmd "ninth 0")

#|
-----------------------------------------------------------------------------
EXAMPLE STELLA INTERACTION
-----------------------------------------------------------------------------

Stella [Top-Level]: mix ninth
Start time offset:(<cr>=None) 
Csound Version 3.42 (Sep 14 1996)
orchname:  /tmp/csound-ex.orc
scorename: /tmp/csound-ex.sco
sorting score ...
	... done
orch compiler:
21 lines read
	instr	1	
MIT Csound: 3.42 (Sep 14 1996)
orch now loaded
displays suppressed
audio buffered in 1024 sample-frame blocks

 PMLqueuesize = 1024
writing 2048-byte blks of shorts to dac (AIFF)
SECTION 1:
ftable 1:
end of section 1	 sect peak amps:      0.0
inactive allocs returned to freespace
SECTION 2:
new alloc for instr 1:
end of section 2	 sect peak amps:  16350.7
end of score.		   overall amps:  16350.7
	   overall samples out of range:        0
0 errors in performance
259 2048-byte soundblks of shorts written to dac (AIFF)
T
Stella [Top-Level]: 
-----------------------------------------------------------------------------
|#



;;;
;;; 6) Re-import the score file into Stella if desired.  
;;;   
;;;    Although Stella does not yet fully support importing of .sco files,
;;;    you can always define "i1" to be a subclass of "beep" and then import.

;; (defobject i1 (beep) ())
;; (import-cmd "/tmp/csound-ex.sco")

#|
-----------------------------------------------------------------------------
EXAMPLE STELLA INTERACTION
-----------------------------------------------------------------------------
Stella [Top-Level]: (defobject i1 (beep) ())
#<STANDARD-CLASS I1>
Stella [Top-Level]: import /tmp/csound-ex.sco 
Stella [Top-Level]: list
Top-Level:
     1. #<ALGORITHM: Ninth>
     2. #<THREAD: From-Csound-Ex> 
Stella [Top-Level]: list 2
From-Csound-Ex:
     1. #<i1 0.374 0.374 0.499 279.627 0.05 0.249> 
     2. #<i1 0.375 0.374 0.499 279.627 0.05 0.249> 
     3. #<i1 0.376 0.374 0.499 299.228 0.05 0.249> 
     4. #<i1 0.37399995 0.374 0.499 341.995 0.05 0.249> 
     5. #<i1 0.375 0.374 0.499 341.995 0.05 0.249> 
     6. #<i1 0.37600005 0.374 0.499 299.228 0.05 0.249> 
     7. #<i1 0.37400007 0.374 0.499 279.627 0.05 0.249> 
     8. #<i1 0.375 0.374 0.499 243.664 0.05 0.249> 
     9. #<i1 0.375 0.374 0.499 211.625 0.05 0.249> 
    10. #<i1 0.375 0.374 0.499 211.625 0.05 0.249> 
    11. #<i1 0.37599993 0.374 0.499 243.664 0.05 0.249> 
    12. #<i1 0.375 0.374 0.499 279.627 0.05 0.249> 
    13. #<i1 0.5619998 0.562 0.499 279.627 0.05 0.249> 
    14. #<i1 0.18700027 0.187 0.499 243.664 0.05 0.249> 
    15. #<i1 0.749 0.749 0.499 243.664 0.05 0.249> 
Stella [Top-Level]: 
-----------------------------------------------------------------------------
|#

)

;;;
;;; -*- EOF -*-

--PART-BOUNDARY=.19701121805.ZM27393.stanford.edu--