Paste: music help 2

Author: typemore
Mode: factor
Date: Thu, 23 Sep 2010 08:27:52
Plain Text |
USING: accessors timers audio audio.engine audio.loader calendar
destructors io kernel locals math math.functions math.ranges specialized-arrays
sequences random math.vectors fry ;
FROM: alien.c-types => short ;
SPECIALIZED-ARRAY: short
IN: music

TUPLE: noise-generator
  { offset initial: 0 }
  { hertz initial: 440 }
  { rate initial: 8000 }
  ;
  
M: noise-generator generator-audio-format
    drop 1 16 8000 ;
M:: noise-generator generate-audio ( generator -- c-ptr size )
    generator rate>> iota
    3.1415926 generator rate>> generator hertz>> / / 2 * '[ _ * sin 4096 * ] short-array{ } map-as
    generator generator rate>> generator hertz>> mod >>offset nip
    8000 ;
M: noise-generator dispose
    drop ;
    
:: audio-test ( -- )
    0 :> i!
    f 4 <audio-engine> :> engine
    engine start-audio*
    
    engine T{ audio-source f { 0.0 0.0 0.0 } 1.0 { 0.0 0.0 0.0 } f }
        noise-generator new
          440 >>hertz
          8000 >>rate
          0 >>offset
        2 
        play-streaming-audio-clip :> noise-clip
        
    [   
        noise-clip source>>
        drop
        engine update-audio
    ] 20 milliseconds every :> timer
    "Press Enter to stop the test." print
    readln drop
    timer stop-timer
    engine dispose ;

New Annotation

Summary:
Author:
Mode:
Body: