Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 5: The Ring Buffer Ken Greenebaum.

Similar presentations


Presentation on theme: "Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 5: The Ring Buffer Ken Greenebaum."— Presentation transcript:

1 Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 5: The Ring Buffer Ken Greenebaum

2 Copyright 2004 Ken Greenebaum SIGFIGS and Sine A complicated analysis: A complicated analysis: sin(x) with x having a certain precision (+/- some value) sin(x) with x having a certain precision (+/- some value) Results in a different range of results Results in a different range of results Depending on the slope of sine at that point Depending on the slope of sine at that point sin(x) with x near 0 sin(x) with x near 0 Result changes faster Result changes faster Slope is vertical Slope is vertical sin(x) with x near Pi/2 sin(x) with x near Pi/2 Result changes slower Result changes slower Slope is horizontal Slope is horizontal

3 Copyright 2004 Ken Greenebaum SIGFIGS and Sine Slope of Sin(x) = Cos(x) Slope of Sin(x) = Cos(x) Sin(x +/- e) Sin(x +/- e) = Sin(x) +/- e * cos(x) = Sin(x) +/- e * cos(x) Have to solve this to determine the SIGFIGS in the final result Have to solve this to determine the SIGFIGS in the final result Pick the lowest number of SIGFIGS Pick the lowest number of SIGFIGS

4 Copyright 2004 Ken Greenebaum SIGFIGS and Sine Example: Sin(2.3) Example: Sin(2.3) 2.3 has 2 SIGFIGS 2.3 has 2 SIGFIGS Really 2.3 +/- 0.1 Really 2.3 +/- 0.1 Slope at Sin(2.3) Slope at Sin(2.3) Cos(2.3) = -0.6663 Cos(2.3) = -0.6663 Sin(2.3) = 0.7457 Sin(2.3) = 0.7457 0.7457 +/- 0.1 * 0.6663 0.7457 +/- 0.1 * 0.6663 0.7457 +/- 0.0666 0.7457 +/- 0.0666 0.6791 - 0.8123 0.6791 - 0.8123 = 0.7 (one sigfig) = 0.7 (one sigfig)

5 Copyright 2004 Ken Greenebaum Assignment What did we learn? What did we learn? Where you surprised by the results? Where you surprised by the results?

6 Copyright 2004 Ken Greenebaum Assignment Regular but not perfect values Regular but not perfect values Might ‘miss’ values Might ‘miss’ values MAX, MIN MAX, MIN Zeros Zeros

7 Copyright 2004 Ken Greenebaum Assignment Solution: void sineSynth(double frequency, int frameRate, int numBits) { double theta; double theta; double frameTime = 1.0/frameRate; double frameTime = 1.0/frameRate; int scale = 0.5 * pow(2, numBits) - 1; int scale = 0.5 * pow(2, numBits) - 1; for(theta = 0.0;; theta = theta+frameTime) for(theta = 0.0;; theta = theta+frameTime) printf("%d\n",(int)( printf("%d\n",(int)( scale*sin(2*PI*frequency*theta))); scale*sin(2*PI*frequency*theta)));}

8 Copyright 2004 Ken Greenebaum Assignment Solution: > sinesynth 2 20 8 | more 074120120740-74-120-120-740

9 Copyright 2004 Ken Greenebaum Assignment Clarification Symmetry Symmetry +/- 2^nBits – 1 +/- 2^nBits – 1 Yes, one 2’s compliment value not reachable Yes, one 2’s compliment value not reachable Unless Unless Add DC offset Add DC offset

10 Copyright 2004 Ken Greenebaum Assignment Questions Questions

11 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Everybody taken data structures? Everybody taken data structures? Mutexes? Mutexes? Questions on the reading? Questions on the reading?

12 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Computing is bursty Computing is bursty I/O is very bursty I/O is very bursty Always waiting for: Always waiting for: Human to type something (1 second) Human to type something (1 second) Process to run again (10 th second) Process to run again (10 th second) Disk to seek (1/200 th second) Disk to seek (1/200 th second) Next audio sample to be needed (1/44100 th second) Next audio sample to be needed (1/44100 th second) Cache line to fill (??) Cache line to fill (??) Next instruction to issue (1/4,000,000,000 second) Next instruction to issue (1/4,000,000,000 second)

13 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue (modern) CPU is very fast compared to audio rates (modern) CPU is very fast compared to audio rates 4,000,000,000 instructions per second 4,000,000,000 instructions per second 100,000 samples per second (48KHz stereo) 100,000 samples per second (48KHz stereo) Potentially can perform 4,000 instructions/sample Potentially can perform 4,000 instructions/sample Or professional audio in less than 1000 th of the CPU Or professional audio in less than 1000 th of the CPU Or 1000 streams of professional audio Or 1000 streams of professional audio

14 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Fast CPU would be doing a lot of waiting w/o Fast CPU would be doing a lot of waiting w/o Multitasking Operating System Multitasking Operating System Can do meaningful work while waiting for I/O to complete Can do meaningful work while waiting for I/O to complete The magic of blocking I/O The magic of blocking I/O BUFFERING! BUFFERING!

15 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Farmer’s coal stove analogy: Farmer’s coal stove analogy: Critical not to allow the fire to go out Critical not to allow the fire to go out Coal deliveries are Coal deliveries are Expensive (Large) Expensive (Large) Erratic Erratic Frequently need to add coal to the stove Frequently need to add coal to the stove Old coal is difficult to light Old coal is difficult to light

16 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Coal SILO/FIFO Queue:

17 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Silo Solves the problems Silo Solves the problems Can accept large (economical) infrequent coal deliveries Can accept large (economical) infrequent coal deliveries Coal is used (more or less) in order of delivery Coal is used (more or less) in order of delivery Farmer can even automatically add coal to stove and not have to fear underflow (running out of coal) Farmer can even automatically add coal to stove and not have to fear underflow (running out of coal)

18 Copyright 2004 Ken Greenebaum Ring Buffer FIFO Queue Audio is very analogous to coal Audio is very analogous to coal Need one sample (relatively) infrequently Need one sample (relatively) infrequently Infrequent for the CPU Infrequent for the CPU (don’t want to wait around for when the next sample is needed) (don’t want to wait around for when the next sample is needed) Frequently for the OS Frequently for the OS (process might not be running when the next sample is needed in 23 micro seconds) (process might not be running when the next sample is needed in 23 micro seconds) Audio must be delivered in strict order! Audio must be delivered in strict order! DAC (furnace) can be automatically filled DAC (furnace) can be automatically filled

19 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue How many have studied: How many have studied: Data structures Data structures multi-threading, semaphores and MUTEX’s? multi-threading, semaphores and MUTEX’s? Priority inversion? Priority inversion? Deadlock? Dining philosophers? Deadlock? Dining philosophers?

20 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue FIFO often taught at same time as MUTEX FIFO often taught at same time as MUTEX Clever FIFO implemented w/o MUTEX Clever FIFO implemented w/o MUTEX Requires atomic write Requires atomic write Head and Tail may be independently updated Head and Tail may be independently updated All calculations error conservatively All calculations error conservatively

21 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Many self consistent schemes Many self consistent schemes Mine has: Mine has: Head points to next available data Head points to next available data (Head updated by consumer) (Head updated by consumer) Tail points to next available location to fill Tail points to next available location to fill (Tail updated by producer) (Tail updated by producer) Head chases tail! Head chases tail!

22 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Ring Buffer FIFO: Ring Buffer FIFO:

23 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Ring Buffer Algebra: Ring Buffer Algebra: Samples Available Samples Available (Tail - Head - 1) mod Size (Tail - Head - 1) mod Size Space Available Space Available min((Head - Tail -1) mod Size, high water mark) min((Head - Tail -1) mod Size, high water mark) Empty Buffer when Empty Buffer when Head = TailBuffer Head = TailBuffer Full when Full when (Head - Tail) Mod Size = 1 (Head - Tail) Mod Size = 1

24 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Latency control via watermarks: Latency control via watermarks:

25 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Buffer over time: Buffer over time:

26 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Key Concepts: Key Concepts: Hysteresis (implications in thermostat) Hysteresis (implications in thermostat) Latency Latency Controlled by high water mark Controlled by high water mark Opportunities in latency Opportunities in latency Elasticity - Variable latency Elasticity - Variable latency Importance of the low water mark Importance of the low water mark Unblocking in time to re-fill Unblocking in time to re-fill

27 Copyright 2004 Ken Greenebaum Introduction to the Ring Buffer FIFO Queue Knowing the future by living in the (near) past: Knowing the future by living in the (near) past:

28 Copyright 2004 Ken Greenebaum Assignment #2 Very Easy Very Easy Requires only 4 new lines: Requires only 4 new lines: Header, buffer declaration, OpenAudioStream(), WriteAudioStream() Header, buffer declaration, OpenAudioStream(), WriteAudioStream() Port Assignment #1 to PABLIO Port Assignment #1 to PABLIO Since sin() returns a float Since sin() returns a float use PABLIO’s paFloat32 use PABLIO’s paFloat32

29 Copyright 2004 Ken Greenebaum Assignment #2 gcc sine.c -L. -lpablio -lWINMM gcc sine.c -L. -lpablio -lWINMM

30 Copyright 2004 Ken Greenebaum Readings Audio Anecdotes Audio Anecdotes PABLIO: A Simple Audio I/O Library PABLIO: A Simple Audio I/O Library Perceivable Audio Latencies Perceivable Audio Latencies

31 Copyright 2004 Ken Greenebaum Next class: More advanced I/O More advanced I/O Perception of Latency Perception of Latency


Download ppt "Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 5: The Ring Buffer Ken Greenebaum."

Similar presentations


Ads by Google