s c h e m a t i c s : c o o k b o o k

/ Cookbook.CreatingAiffs

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Creating simple AIFF audio files

Problem

DrScheme seems to have little support for sound.

Solution

Presented here, sample code for writing 16 bit monophonic AIFF audio files, and a few procedures for sound synthesis in order to test it. AIFF files are lossless Interchange File Format-derived audio files used by Apple and SGI, and should hopefully be playable on any modern computer. The reader is advised to consult the internet for the intimate details of AIFF specifically, and the general concepts of IFF. In short, they are a binary format containing nested "chunks" of data. Each "chunk" consists of a 4 character ID tag, followed by 4 bytes representing the chunk size, then the chunk's contents.

A minimal AIFF file contains one top-level chunk, with the ID of "FORM". This contains inside it a 4 byte type of "AIFF" followed by two chunks, "COMM" and "SSND". COMM, the Common chunk, holds information on the bit size, sample frame rate, and so forth. SSND contains a bitstream representing the audio.

Upon running this recipe, it will generate 20 seconds of complex audio and write it to a file called "test.aiff", which will be monaural, 16 bit, and at 22kHz sample rate.

enchunk is a general-purpose function for encoding IFF chunks.

buffer->aiff converts a bytestring of sample frames into a bytestring representing an entire AIFF file.

sample-to-bytes converts a continuous value between -1 and 1 into a 16 bit sample.

op->buffer converts a function of time representing a waveform ("operator") into a buffer (bytestring) representing a waveform.

Other procedures and constants are essentially just defined for amusement value!

%begin scheme%
;;uses swindle and PLT Scheme

;;some constants - rates in Hz and as aiff bytestrings
(define rate44kHz (cons 44100 (bytes #xAC #x44 #x00 #x00)))
(define rate22kHz (cons 22255 (bytes #x56 #xEE #x8B #xA3)))
(define A440 440) ;ISO
(define A480 480) ;Bach organ

;;useful
(define (linear-pitch-space p &opt (middleA A440))
  (define (log2 n) (/ (log n) (log 2)))
  (expt 2
        (+ (/ (- p 69)
              12)
           (log2 middleA))))

;;-1<x<1 to 16 bit int
(define (sample-to-bytes x)
  (integer->integer-bytes (inexact->exact (floor (* x 32000))) 2 #t #t))

;;pack data into an IFF chunk
(define (enchunk ID buffer &opt (prefix (bytes)))
  (let* ((l (+ (bytes-length buffer)
               (bytes-length prefix)))
         (nobytes (bytes))
         (padded (if (odd? l)
                     (+ 1 l)
                     l)))
    (if (eq? (string-length ID) 4)
        (bytes-append (string->bytes/locale ID)
                      (integer->integer-bytes padded 4 #f #t)
                      (if prefix prefix nobytes)
                      buffer
                      (if (odd? l) (bytes 0) nobytes))
        (error "ID must be 4 characters long!"))))

;;make a buffer of sample frames based on an operator of length t
(define (op->buffer operator t &opt (rate rate44kHz))
  (let ((buffer (make-bytes (* 2
                               (+ 1 (* t (car rate))))))
        (end t)
        (inc (/ 1 (car rate))))
    (let loop ((x 0)
               (byteoffset 0))
      (if (> x end)
          buffer
          (begin (bytes-copy! buffer
                              byteoffset
                              (sample-to-bytes (operator x)))
                 (loop (+ x inc) (+ byteoffset 2)))))))

;;buffer to aiff bytestring
(define (buffer->aiff bitstream &opt (rate rate44kHz))
  (let ((frames (/ (bytes-length bitstream) 2)))
    ;;FORM chunk contains other chunks
    (enchunk "FORM"
             (bytes-append
              ;COMMon block
              (enchunk "COMM"
                       (bytes-append 
                        (integer->integer-bytes 1 2 #t #t) ;channels (mono)
                        (integer->integer-bytes frames 4 #f #t) ;sample frames (16)
                        (integer->integer-bytes 16 2 #t #t) ;bitwidth
                        (bytes #x40 #x0E) ;no idea
                        (cdr rate) ;framerate
                        (bytes 0 0 0 0))) ;yet more WTFery.
              ;Sound data block
              (enchunk "SSND"
                       bitstream
                       (make-bytes 8 0))) ;;quick hack to ignore some unused settings
             ;;Name FORM type
             (string->bytes/locale "AIFF"))))

;;
;;Some operators
;;

;;Sinewaves
(define (sinewave freq)
  (lambda (x) (sin (* x freq 6.2832))))

;;whitenoise
(define (noise)
  (lambda (x) (random)))

;;VCA
(define (ampmod signal mod)
  (lambda (x) (* (signal x)
                 (mod x))))

;;half-wave
(define (half-wave op)
  (lambda (x)
    (let ((b (op x)))
      (if (> b 0) b 0))))

;;FM
(define (FMoperator freq modulation)
  (lambda (x)
    (sin (+ (* x freq 6.2832)
            (modulation x)))))

;;Mix sound sources
(define (mix &rest sources)
  (lambda (x)
    (/ (foldl (lambda (operation sum)
                (+ sum
                   (operation x)))
              0
              sources)
       (length sources))))

;;Amplitude tweak. Will clip cleanly if overdriven.
(define (amp a op)
  (lambda (x)
    (let ((b (* a (op x))))
      (cond ((< b -1) -1)
            ((> b 1) 1)
            (else b)))))


;;
;;generate sound
;;
(define complex-sound (op->buffer (mix (ampmod (mix (sinewave (linear-pitch-space 60))
                                                    (sinewave (linear-pitch-space 64))
                                                    (sinewave (linear-pitch-space 67))) ;C major
                                               (half-wave (amp 10 (FMoperator 8
                                                                              (sinewave 1))))) ;morse effect
                                       (amp 0.3 (ampmod (noise)
                                                        (sinewave 0.08))) ;fading white noise
                                       (ampmod  (amp 10 (FMoperator (linear-pitch-space 58)
                                                                    (sinewave (/ (linear-pitch-space 58) 3)))) ;distorted FM bass
                                                (sinewave 0.01))) ;fade in
                                  20
                                  rate22kHz))

(with-output-to-file "test.aiff"
  (thunk (display (buffer->aiff complex-sound rate22kHz)))
  'replace)

%end%

Discussion

Converting this to write WAV files should be trivial. There seems to be a few omissions in the version of the AIFF spec I was using, hence a few (clearly marked) kludges have been used to get it working. Two sample rate constants have been defined, one for 22kHz and one for 44kHz. Currently, bit depth is hard-wired and only one audio channel is supported (ie. mono). When using linear-pitch-space, a value of 60 represents middle C, 61 represents C sharp, 59 represents B and so forth.

Extensive use is made of extensions provided by Swindle.

AIFF writing is dog-slow, and it doesn't sound like a Moog.

To start experimenting more simply, replace the complex sound mix with (sinewave A440) or (noise) for middle A and white noise respectively.


Comments about this recipe

Contributors

-- RitchieSmith - 15 Aug 2006

CookbookForm
TopicType: Recipe
ParentTopic: FileRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest