Lab 2 Modeling an audio channel with delays
Lab. 2 Modeling an audio channel with delays on ADSP 21061 M. R. Smith, Electrical and Computer Engineering University of Calgary, Alberta, Canada smithmr @ ucalgary. ca
To be tackled today z Essence of Lab. 2 – see web for precise details y Build variants of algorithms for FIFO (Delay Buffer) x. Mass Memory Move (written in “C++” -- provided) x. Mass Memory Move (written in “asm” -- direct translation) x. FIFO using software circular buffer (written in “C++” -- provided) x. FIFO using software circular buffer (written in “asm” -- direct translation) y Test that algorithms work correctly using “OFF-LINE” (using the board in the lab. and the simulator outside) y Time the various algorithms -- How good is “optimizing compiler” compared to hand-coding. y Test the effect in an “audio-sense” using “LOCAL” and CODEC”. Here the effect of “length of time in ISR” becomes important for sound quality z Laboratory 3 -- same as Lab. 2 but using custom DSP features of the processor for implementing “circular buffers” z Details of “main. cpp”, “processsound. c” and audio libraries. 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 2
Labs. 1 and 2 -- Model channel delays O DELAY 1 DELAY 2 z No delay between left/right ear sound arrivals -- then sound perceived in centre of head z Delay introduced into right ear sound channel will shift sound to left as “sound” seems to get to left ear first. 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 3
Sound Source Left Ear Right Ear Sound Source Process. Sound(channel 1_in, channel 2_out, *outleft, *outright) Left Ear 12/15/2021 Right Ear ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 4
Basic Algorithm -- Channel Delays Input LEFT <----- LEFT DELAY -----> Output DELAYED LEFT Input RIGHT <-----DELAY -----> Output DELAYED RIGHT 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 5
Basic Algorithm -- Channel Delay <----- LEFT DELAY -----> <-----DELAY -----> Requires 2 * (LD + RD - 2) memory operations 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 6
Better Algorithm -- Channel Delay <----- LEFT DELAY -----> <----- R. DELAY -----> Requires 2 * (Max(LD, RD) - 2) memory operations Don’t use this approach in Lab. 2, we are trying to look At other issues and other efficiencies 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 7
Physics of the Problem <---- Posn (P) ---> Sound Source Dright = SQRT(D^2 + (P - 0. 1)^2) Dleft = SQRT(D^2 + (P + 0. 1)^2) Dleft Dright D HEAD <----- 20 cm -----> 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 8
Relative Delay Necessary Distance to Sound Time Delay = -------------Speed of Sound Left/Right Delay Algorithm Delay = ------------Sampling Rate Left/Right Delay = (Dleft_distance - Dright_distance) / Speed of Sound Two sound sources -- imply two relative delays Speed of sound = 300 m/s Sampling Rate = 44 k. Hz Calculate Number of Delays needed for D = 0, D = 1 m when sound source position varies (P = 0, P = 0. 1, P = 0. 2)? 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 9
Handle Channel Delay -- Can it be done? <---- LEFT/RIGHT DELAYS -----> “C” Code for channel delay algorithm described in previous lecture Requires 2 * (LD + RD - 2) memory operations of the form R 0 = dm(1, I 4); dm(I 4, 1) = R 0 in loop (Why not use dm(-1, I 4) = dm(I 4, 1) ? ) To avoid audio distortion, the algorithms must be completed within 1/2 of interrupt period when interrupts occurring at 44 k. Hz If have 40 MHz SHARC processor -- 1 cycle per instruction Less than ? ? cycles available 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 10
Modeling Audio Channel Delays using Mass Memory Moves #define MAXDELAY 0 x 80 void Memory. Move_Left. Delay(int process_var 1, int *channel_one) { int count; static int left_delayline[MAXDELAY] = {0}; // Insert new value into the back of the FIFO delay line left_delayline[0 + process_var 1] = *channel_one; // Grab delayed value from the front of the FIFO delay line *channel_one = left_delayline[0]; // Update the FIFO delay line using inefficient memory moves for (count = 0; count < process_var 1; count++) left_delayline[count] = left_delayline[count + 1]; } 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 11
Alternative approach Software Circular Buffer Output POINTER Input POINTER <----- DELAY -----> Output value Input value Output POINTER++ 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca Input POINTER++ 12
Output pt Input pt A 2 A 11 Output pt A 3 Input pt A 12 Output pt Input pt A 4 A 13 Input pt A 14 Output pt A 5 Gets nasty when need to access more than one value from delay line (e. g. during FIR -- Lab. 4 – uses 32 pointer operations) 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 13
Audio channel delay modeled using software circular buffers void Soft. Circular. Buffer_Left. Delay(int process_var 1, int *channel_one) { int count; static int left_delayline[MAXDELAY] = {0}; static int *left_pt_in = &left_delayline[0 + process_var 1]; ? legal static int *left_pt_out = &left_delayline[0]; static int *overflow_pt = &left_delayline[MAXDELAY]; ? why // One out problem? MAXDELAY - 1, MAXDELAY + 1? // Insert new value into the back of the FIFO delay line *left_pt_in = *channel_one; // Grab delayed value from the front of the FIFO delay line *channel_one = *left_pt_out; // Update the FIFO delay line using pointer arithmetic for circ. Buff left_pt_in++; if (left_pt_in >= overflow_pt) left_pt_in = left_pt_in - MAXDELAY; } 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 14
Timing for the algorithms z. Memory moves y. LD + RD - 2 memory access operations y 2 * (LD + RD - 2) instructions roughly y. Expect to find sound distortions for large delays z. Pointers -- software circular buffers y 4 pointer update operations y 4 point checks for out of bounds z. Pointers -- hardware circular buffers y. NO OVERHEAD AT ALL -- Lab. 3 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 15
Prelaboratory 2 -- done in “C” z Check web page for exact details y. Hand-in by 2 p. m. for immediate marking (100% time penalty) z Test “algorithms” for implementing the audio channel delay (Using memory moves and software circular buffers to handle FIFO implementation) y. Simulation -- not hardware y. Model delay in only 2 channel (as ear can’t hear absolute delays only relative delays y. Remember ONLY 20 floating licenses z Test means -- Provide screen dumps showing plots of input, left output and right output array for IMPULSE input for various delays (0, 100, 200 samples) -- Profile information would also be good. 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 16
Lab. 2 -- Audio Channel Delay modeling on ADSP 21061 processor z Build variants of algorithms for FIFO (Delay Buffer) running on the simulator y Mass Memory Move (written in “C++” -- provided) y Mass Memory Move (written in “asm” -- direct translation) y FIFO using software circular buffer (written in “C++” -- provided) y FIFO using software circular buffer (written in “asm” -- direct translation) z For the assembly code routine YOU wrote, calculate the number of cycles spend inside the FIFO routines for delays of size 0, 100 and 200. z Graph the results. Use the graph to “predict” maximum number of delay intervals before will be in FIFO routine for more than half 1/2 of the time available from 44 k. Hz interrupt handler. y Overflow = predicted sound distortion running ON-LINE with a MONO | LEFT_DELAY | LOCAL_SOUND source. 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 17
Lab. 2 -- Audio Channel Delay modeling on ADSP 21061 processor z Profile algorithms on the simulator to determine the actual time spend in the FIFO routines. y Compare to your predictions. (Don’t change (fake) your predictions). y Graph the results. Use the graph to “predict” when distortions would occur in a real time implementation. z Overflow = predicted sound distortion running ONLINE with a MONO | LEFT_DELAY | LOCAL_SOUND source. z Check your predictions by running the LOCAL_SOUND program using MONO | LEFTDELAY options z Write a (short) report analysing the laboratory results. 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 18
Files used in every lab. zmain. c y. Activates the sound sources and audio channel modeling zprocess. c and fasterchannelmodels. asm y. The algorithms used in the audio channel modeling zprocessdefs. h y. Constants used to define the channel modeling operations 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 19
main. c -- Page 1 void main(void) { // Set the WHICHSOURCE option as -D WHICHSOURCE=? ? ? as a COMPILER option // Other options available for sources #if (WHICHSOURCE & LOCAL_SOURCE) // Weird mono and stereo source whichsource = LOCAL_SOURCE | FM_STEREO; #elif (WHICHSOURCE & CODEC_SOURCE) // Hook up CD stereo (need cable) whichsource = CODEC_SOURCE | FM_STEREO; #elif (WHICHSOURCE & OFFLINE_SOURCE) // For testing without the hardware whichsource = OFFLINE_SOURCE | SQUAREWAVE | FM_STEREO; #endif ******* #define DELAYUNITS 44 Attach. Sound. Source(whichsource, ……); My. Set. Up( ) while (Read. Sound. Source(&channel_one, &channel_two) != 0){ Process. Sound(channel_one, channel_two, &left_channel, &right_channel); Write. Sound. Source(left_channel, right_channel); } } 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 20
processdefs. h file -- Constants SOUND SOURCES OFFLINE_SOURCE 0 x 80000001 LOCAL_SOURCE 0 x 80000002 CODEC_SOURCE 0 x 80000004 STEREO MONO FM_STEREO 0 x 00000010 0 x 00000020 0 x 00000040 LEFT_ONLY RIGHT_ONLY LEFT_DELAY RIGHT_DELAY 0 x 00000100 0 x 00000200 0 x 00000400 0 x 00000800 SINEWAVE IMPULSE SQUAREWAVE USERDEFINED 0 x 00001000 0 x 00002000 0 x 00004000 0 x 00008000 12/15/2021 AUDIO CHANNEL CHARACTERISTICS MOVEDELAY 0 x 00010000 SOFTWARE_CIRCBUFFERDELAY 0 x 00020000 HARDWARE_CIRCBUFFERDELAY 0 x 00040000 OVERSIMPLEIIR 0 x 00100000 FIRFILTER 0 x 00200000 IIRFILTER 0 x 00400000 // Use your own defined characteristics USER 1 0 x 01000000 USER 2 0 x 02000000 USER 3 0 x 04000000 USER 4 0 x 08000000 DON’T CHANGE/ADD TO THESE CONSTANTS -- ALL THE UTILITIES RELY ON THEM ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 21
Processsound. cpp -- Page 2 extern int sound_source, process_var 1; // Globals set by Activate. Sound( ) extern float process_var 2; void Process. Sound(int channel_one, int channel_two, int *left_channel, int *right_channel) { if (sound_source & FM_STEREO) Decode. FMSTEREO(channel_two_strength, &channel_one, &channel_two); if (sound_source & OVERSIMPLEIIR) Over. Simple. IIR(/* IIRcoeff */ process_var 2, &channel_one, &channel_two); if ((sound_source & LEFT_DELAY) == LEFT_DELAY) { if ((sound_source & MOVEDELAY) == MOVEDELAY) Memory. Move_Left. Delay(process_var 1, &channel_one); else if ((sound_source & SOFTWARE_CIRCBUFFERDELAY) { Soft. Circular. Buffer_Left. Delay(process_var 1, &channel_one); } else if (sound_source & HARDWARE_CIRCBUFFERDELAY) { printf("Hardware Circular buffer delay_line operations defined in Lab. 2"); exit(0); } } // Have finished modifying the channels *left_channel = channel_one; *right_channel = channel_two; if ((sound_source & RIGHT_ONLY) *left_channel = 0; } 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 22
Switching between “C” version and “assembly code” version z Run the “C++” code and get working. z Make a copy of the “C++” code into file lab 1. asm y. Turn all code into comment statements y. Do a “ 1 -to-1” translation of “C” into assembly code. Apply your PSP to avoid the standard errors. y. Assemble/link and record syntax errors that need to be added to PSP process. z See the lab. notes for a “quick and easy” process to switch back and forth between C++ and assembly code 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 23
Another useful utility z. Visual. DSP has the ability to “profile” how long you spent accessing instructions in any given range of memory z. Identify the start and end of your program – start and end of a loop z. Profile the code y. Only makes sense when you know “exactly” how many times the code is being executed in theory 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 24
Profiling -- Adding a range z. Ranges can be in PM or DM memory z. Don’t forget to enable profiling and THEN run the program z. Don’t profile the call from main( ) 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 25
Profiling a range -- 2 -- Find Start 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 26
Profiling a range -- 3 -- Find End 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 27
Analysis of Profile Range -- 4 Select VIEW | DEBUG |PROFILE 12/15/2021 ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 28
Analysis of Profile Range -- 5 Tells us very little, except that only small part of this program (768 points only) Cycles -- time including clashes? Read Count and Write Count? 12/15/2021 Exec Count -- instructions Read? ENCM 515 -- SHARC Audio Channel Modeling in Lab 1 Copyright M. Smith -- smithmr@ucalgary, ca 29
- Slides: 29