defaultxr / cl-patterns

Library for writing patterns to generate or process (a)musical sequences of mathematically (un)related (non-)compound values in Lisp.
https://w.struct.ws/cl-patterns
MIT License
77 stars 10 forks source link

Saving output to file #35

Closed jagrg closed 2 years ago

jagrg commented 2 years ago

How can I save an excerpt of the output to an audio file? For example, using this example:

(ql:quickload :cl-collider)
(in-package #:cl-collider)

(setf *s* (make-external-server "localhost" :port 4444))
(server-boot *s*)
(jack-connect)

(defsynth beep ((gain 1))
  (let* ((env (line.kr 4 0 .03 :act :free))
         (sig (sin-osc.ar 500 0 env)))
    (out.ar 0 (pan2.ar sig 0 gain))))

(ql:quickload :cl-patterns/supercollider)
(cl-patterns:backend-start :supercollider)
(in-package #:cl-patterns)
(start-clock-loop :tempo 110/60)

(pb :beat
  :embed (pcycles "x-xx-x-xx-xx" :dur 3)
  :instrument :beep)

(play (list :beat))
defaultxr commented 2 years ago

Sorry for the delay in responding, I wanted to make sure everything was actually working first and it turned out things weren't.

I've fixed most of the issues now, so ensure you're using a local copy of cl-patterns updated to the latest commit on master, and then you can render a pattern with render like so:

(render (pdef :beat) "/path/to/output.wav" :duration 4) ; render 4 seconds of audio

(render (pdef :beat) "/path/to/output.wav" :dur 4) ; render 4 beats of audio

(render (pdef :beat) "/path/to/output.wav" :max-length 4) ; render the first 4 events of the pattern (note that rests are included)

Since your :beat pattern is infinite in length, you need to specify :duration, :dur, or :max-length if you want the rendered output to be a specific length.

Alternatively you can make it finite in length, and as long as it has fewer than :max-length (which defaults to *max-pattern-yield-length*) outputs, the full pattern will be rendered without needing to specify any keyword arguments.

I've tested this new implementation a bit and it seems to work, but there are probably some cases where it might still be broken. Let me know if you notice any.

jagrg commented 2 years ago

FYI it fails when the path is relative. Works fine otherwise. Many thanks!