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

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
HW/Study Guide. Synchronization Make sure you understand the HW problems!
COS Operating System Assignment 4 (Precept 2) Inter-Process Communication and Process management Fall 2004.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Secure Operating Systems Lesson 5: Shared Objects.
Speech Compression. Introduction Use of multimedia in personal computers Requirement of more disk space Also telephone system requires compression Topics.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
1 Concurrency: Deadlock and Starvation Chapter 6.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
SE320: Introduction to Computer Games Week 8: Game Programming Gazihan Alankus.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 17:Wavetable Synthesis Ken Greenebaum.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Recall: Three I/O Methods Synchronous: Wait for I/O operation to complete. Asynchronous: Post I/O request and switch to other work. DMA (Direct Memory.
External Sorting Sort n records/elements that reside on a disk. Space needed by the n records is very large.  n is very large, and each record may be.
Consider the Java code snippet below. Is it a legal use of Java synchronization? What happens if two threads A and B call get() on an object supporting.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
Copyright ©: University of Illinois CS 241 Staff1 Threads Systems Concepts.
Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 24:Environment Ken Greenebaum.
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 6: Wrapped I/O Ken Greenebaum.
Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 18:Noise Ken Greenebaum.
Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 14: Envelopes Ken Greenebaum.
Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 20:Spectral Filtering Ken Greenebaum.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Discussion Week 2 TA: Kyle Dewey. Overview Concurrency Process level Thread level MIPS - switch.s Project #1.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Copyright 2004 Ken Greenebaum Introduction to Interactive Sound Synthesis Lecture 4: Interfaces Ken Greenebaum.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Where Testing Fails …. Problem Areas Stack Overflow Race Conditions Deadlock Timing Reentrancy.
MULTITHREADED PROGRAMMING Processes vs. Threads Process states and state transitions Hazards and Semaphores 1.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Interprocess Communication Race Conditions
Data Structures Using C, 2e
Queues.
CS703 – Advanced Operating Systems
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Chapter 5: Process Synchronization – Part 3
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Background on the need for Synchronization
Process Synchronization
Chapter 5: Process Synchronization – Part II
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Operating Systems CMPSC 473
Chapter 5: Process Synchronization (Con’t)
Chapter 9: Virtual-Memory Management
Semaphore Originally called P() and V() wait (S) { while S <= 0
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Computer Science & Engineering Electrical Engineering
CSCI1600: Embedded and Real Time Software
Assignment 6 Recitation
CSCI1600: Embedded and Real Time Software
Presentation transcript:

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

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

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

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) = Cos(2.3) = Sin(2.3) = Sin(2.3) = /- 0.1 * /- 0.1 * / / = 0.7 (one sigfig) = 0.7 (one sigfig)

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?

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

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)));}

Copyright 2004 Ken Greenebaum Assignment Solution: > sinesynth | more

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

Copyright 2004 Ken Greenebaum Assignment Questions Questions

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?

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)

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

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!

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

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

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)

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

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?

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

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!

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

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

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

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

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

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:

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

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

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

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