]> gitweb.factorcode.org Git - factor.git/blob - extra/synth/synth.factor
interpolate: split out format into a hook
[factor.git] / extra / synth / synth.factor
1 ! Copyright (C) 2008 Alex Chapman
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math math.constants math.functions
4 sequences sequences.modified sequences.repeating ;
5 IN: synth
6
7 SLOT: sample-freq
8
9 MEMO: single-sine-wave ( samples/wave -- seq )
10     [ <iota> ] [ pi 2 * swap / [ * sin ] curry ] bi map ;
11
12 : (sine-wave) ( samples/wave n-samples -- seq )
13     [ single-sine-wave ] dip <cycles> ;
14
15 : sine-wave ( sample-freq freq seconds -- seq )
16     pick * >integer [ /i ] dip (sine-wave) ;
17
18 : >sine-wave-buffer ( freq seconds buffer -- buffer )
19     [ sample-freq>> -rot sine-wave ] keep swap >>data ;
20
21 : >silent-buffer ( seconds buffer -- buffer )
22     [ sample-freq>> * >integer 0 <repetition> ] [ data<< ] [ ] tri ;
23
24 TUPLE: harmonic n amplitude ;
25 C: <harmonic> harmonic
26
27 TUPLE: note hz secs ;
28 C: <note> note
29
30 : harmonic-freq ( note harmonic -- freq )
31     n>> swap hz>> * ;
32
33 :: note-harmonic-data ( harmonic note buffer -- data )
34     buffer sample-freq>> note harmonic harmonic-freq note secs>> sine-wave
35     harmonic amplitude>> <scaled> ;
36
37 : >note ( harmonics note buffer -- buffer )
38     [ [ note-harmonic-data ] 2curry map <summed> ] [ data<< ] [ ] tri ;