CS245 Introduction to Interactive Sound Synthesis

Introduction to Interactive
Sound Synthesis
Lecture 6: Wrapped I/O
Ken Greenebaum
Copyright 2004 Ken Greenebaum
Assignment 1


Most people got it completely
Most common mistake missing the:

scale = 0.5 * pow(2, numBits) - 1;
Copyright 2004 Ken Greenebaum
Assignment 2

Questions?
Copyright 2004 Ken Greenebaum
Assignment 2

4 new lines:
1.
2.
3.
4.
#include "pablio.h“
float buffer[1];
OpenAudioStream(&outStream,
48000, paFloat32,
PABLIO_WRITE|PABLIO_MONO);
WriteAudioStream(outStream,
buffer, 1);
Copyright 2004 Ken Greenebaum
Assignment 2

2 lines modified:
1.
2.
double frameTime = 1.0/48000;
buffer[0] =
(float)sin(2*PI*frequency*theta);
Copyright 2004 Ken Greenebaum
Assignment 2

Stereo?


Default mode
Multiplexed L,R


Buffer[0], Buffer[1]
1 Frame = 2 Samples
Copyright 2004 Ken Greenebaum
Assignment 2

You did it!



Sampling/Quantization/Synthesis
Audio Out
We can do anything now (and will)

Next assignment:
Collision synthesizer
 Using additive synthesis



Wood, glass, bells
Will get us thinking about the mixer to come
Copyright 2004 Ken Greenebaum
3%
3%
3%
3%
Ring Buffer Review
3%
3%
3%
3%
3%

3%
Ring Buffer FIFO:
3%
3%
3%
3%
3%
3%
3%
3%
3%
Direction of pointer
increment
3%
Head (next value to be
removed)
3%
3%
3%
Tail (next cell to be filled)
is a test
Copyright 3%
2004this
Ken
Greenebaum
3%
0
Ring Buffer Review

Ring Buffer Algebra:

Samples Available


Space Available


min((Head - Tail -1) mod Size, high water mark)
Empty Buffer when


(Tail - Head - 1) mod Size
Head = TailBuffer
Full when

(Head - Tail) Mod Size = 1
Copyright 2004 Ken Greenebaum
Introduction to the Ring Buffer
FIFO Queue

Latency control via watermarks:
Copyright 2004 Ken Greenebaum
Introduction to the Ring Buffer
FIFO Queue

Buffer over time:
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
0
3%
0
0
this is a test
3%
3%
3%
3%
this is a test
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
3%
this is a test
3%
3%
3%
3%
3%
3%
Copyright 2004 Ken Greenebaum
3%
3%
3%
3%
3%
3%
Introduction to the Ring Buffer
FIFO Queue

Key Concepts:


Hysteresis (implications in thermostat)
Latency
Controlled by high water mark
 Opportunities in latency
 Elasticity - Variable latency


Importance of the low water mark

Unblocking in time to re-fill
Copyright 2004 Ken Greenebaum
Introduction to the Ring Buffer
FIFO Queue

Knowing the future by living in the (near)
past:
Copyright 2004 Ken Greenebaum
Wrapped I/O:

Questions from reading?
Copyright 2004 Ken Greenebaum
Wrapped I/O:

Benefits to wrapping ring buffer in API:

Data abstraction


Enforce uniform access


Allows data structure to change
Contract allows ring buffer algebra to work
Code re-use
Every application doesn’t have to re-create the
wheel
 (Potentially inconsistently!)

Copyright 2004 Ken Greenebaum
Wrapped I/O:

Issues with wrapping ring buffer in API:

Extra copy required


Actually Read and Write req’d anyway
Perhaps limit possibilities:

Move H or T w/o modifying samples
Copyright 2004 Ken Greenebaum
Buffer Issues

D/A, A/D are realtime devices



Underflow



Can’t be blocked
Need/Generate Frame every 1/FrameRate
seconds
D/A requires next sample but RB empty
Producer wasn’t run in time to meet deadline
Overflow


A/D produces next sample but RB is full
Consumer wasn’t run in time to meet deadline
Copyright 2004 Ken Greenebaum
Buffer Issues

Underflow, Overflow result:

Dropped samples

Lost forever
Annoying clicks, pops
 Very Annoying sound loop

Copyright 2004 Ken Greenebaum
Underflow Policies

Stop (silently) + wait for the missing data


Continue immediately


Application: Radio talk show
Application: Playing music
Give up and start all over again

Application: Mastering CD
Copyright 2004 Ken Greenebaum
Underflow Policy

Quiescent underflow




System provides zero’s (silence) on underflow
SGI AL, PABLIO
Consumer zero’s samples after they are used
Non-Quiescent underflow


DirectSound
Many (most?) libraries
Copyright 2004 Ken Greenebaum
Quiescent Underflow Uses

Sound injection


Hold sound stream open
Only inject (brief) sounds as desired
No need for audio process to wait and flush the
samples out
 Sound may played at time with extreme precision

Copyright 2004 Ken Greenebaum
Stricter Underflow Policy

Underflow notification required:

Recording/Mastering application


Master failed, need to re-record
Debugging/Tuning realtime application
Perhaps human tuning
 Perhaps automatic, dynamic tuning




Using high-water mark
Fallback to higher latency
Fallforward?
Copyright 2004 Ken Greenebaum
Aliasing



Underflow must be detected in audio system
Did the pointer advance by one sample?
Or loop completely?
Copyright 2004 Ken Greenebaum
Detecting Aliasing in User-space



Attempt to monitor underflow/aliasing
Using large buffer:
(underflow?)
Copyright 2004 Ken Greenebaum
Detecting Aliasing in User-space

May also use another clock source


Issue


CPU’s high resolution timer
Another clock isn’t locked to D/A
Overcome using synchronization techniques
Copyright 2004 Ken Greenebaum
Does your computer drop samples?



Well does it?
How do you know?
What can you do about it?
Copyright 2004 Ken Greenebaum
Ramptest

Output a known signal


What signal?
Why?

Loopback outback to input

Analyze the signal received


Best if digital loopback
What conditions to test for?
Copyright 2004 Ken Greenebaum
Latency as a cure-all?




Many audio systems will increase buffering
in an attempt to provide more robustness
How much buffering in your system?
How to determine this?
What are the costs of latency?
Copyright 2004 Ken Greenebaum
Canonicalization/Mixing

Hardware probably has native format






16 bit 2’s complement stereo?
Nice if system accepts any format
Easiest to mix like streams
Copy opportunity to canonicalize format
Can attenuate
Can even re-sample at this time too
Copyright 2004 Ken Greenebaum
Underflow in a Mixer

What should happen if one stream of a
mix underflows?
1.
2.


Underflow the result of the mix?
Allow the mix to continue minus the one
channel?
AL does #2
What does Direct Sound do?

Why?
Copyright 2004 Ken Greenebaum
Overflow in a Mixer

Opposite of a mixed output



Fanned out input
Input signal provided to many consumers
simulteneously
Why would this be desirable?
Copyright 2004 Ken Greenebaum
Fan-out

Simultaneous Examples:




Audio recorder
Audio level meter
Voice recognizer
What should happen if one stream in a
fanout backs up?
Copyright 2004 Ken Greenebaum
Overflow in Fan-out


AL provides input to all inputs
Only the overflowing buffer loses samples
Copyright 2004 Ken Greenebaum
Readings:

Audio Anecdotes

Perceivable Audio Latencies
Copyright 2004 Ken Greenebaum
Next class:



Perception of Latency
Additive Synthesis
Next assignment: Collision Synth
Copyright 2004 Ken Greenebaum