Audio Fundamentals - Basics

Audio Fundamentals - Basics

Slides for a workshop on audio fundamentals.

Bb5482b3806c6fe891e6270b99597a71?s=128

Chinmay Pendharkar

May 10, 2013
Tweet

Transcript

  1. Audio Fundamentals Chinmay Pendharkar, Sonoport, 2013

  2. None
  3. Sound Waves

  4. Analog vs Digital Audio

  5. Analog vs Digital Audio

  6. Digitization

  7. Digitization - Why? • Easier to manipulate digital audio •

    Easier to store digital audio • Easier to transport digital audio • Get back perfect original analog audio
  8. Digital Audio Analog Audio Analog - Digital Conversion Digital -

    Analog Conversion Digital Audio
  9. Sampling Frequency

  10. Sampling Frequency

  11. Sampling Theorem

  12. Sampling Theorem

  13. Sampling Theorem 2 x (Max Frequency of Content) ≤ Sampling

    Frequency for perfect reconstruction of analog signal from digital
  14. Sampling Frequency 2 x (Max Frequency of Content) ≤ Sampling

    Frequency. 2 x ( 20000 Hz ) ≤ 44100 Hz . Upper Limit of Human Hearing
  15. Sampling • Sampling Frequency > 44100 : Oversampling • Sampling

    Frequency < 44100 : Undersampling • 44100Hz => ◦ 44100 samples per second ◦ 44100 data points per second • Less computation vs Audio Fidelity
  16. Quantization / Bit Depth uint_16

  17. Quantization 0x0000 (0) 0xFFFF (65535) 96dBFS ~ 120dB Human Hearing

    Range 65535 65534 65533 65532 0
  18. Digital Audio Discretization of Time - Sampling Discretization of Amplitude

    - Quantization
  19. Digital Audio Common Formats: Sampling Rate : 44100 Hz Bit

    Depth : 16 bits Sampling Rate : 192000 Hz Bit Depth : 24 bits "CD Audio" "Mastering Quality"
  20. Digital Audio Formats • Uncompressed ◦ AIFF ◦ WAV •

    Compressed (Lossless) ◦ FLAC ◦ ALAC • Compressed (Lossy) ◦ MP3 ◦ AAC
  21. Real Time Audio on PC/Mac

  22. Audio Stacks Audio Hardware Drivers Operating System Audio API User

    Program/Application
  23. Audio Stack - Windows

  24. Audio Stack - OSX/iOS

  25. Audio Stack - Linux

  26. Audio Stack - Android

  27. Audio Stacks Audio Hardware Drivers Operating System Audio API User

    Program/Application
  28. Audio Stack Audio Hardware DAC Digital Analog Converter

  29. Audio Stacks 44.1kHz / 16bits ~= 172 kB per second

    DAC Digital Data Buffer "Magic" OS
  30. DAC Schemes DAC Digital Data Buffer #1 OS Digital Data

    Buffer #2 "Magic" 1s
  31. Buffer Size - Latency • Time taken = Max time

    to fill next buffer • Time taken = Min time to new sound output Stereo = 2 channels
  32. Buffer Size - Latency Ideally we want.... • Fill up

    next buffer well before current is fully output. (No clicks/silence) • Have no delay between audio being sent to the OS and output. (No buffering)
  33. Buffer Size - Latency Typical Buffer Sizes • 256 *

    2 * 2 = 1024 bytes = 5.8ms latency • 512 *2 * 2 = 2048 byes = 11.6ms latency • 1024 * 2 * 2 = 4096 byes = 23.2ms latency Assuming 2 channel stereo and 16bit depth
  34. Callback vs R/W IO Two common schemes for audio drivers.

    • Read-Write • Callback
  35. Read-Write • Direct R/W access to "digital data buffer". •

    Blocking • PaError Pa_WriteStream ( PaStream * stream, void * buffer, long frames ) • PaError Pa_ReadStream ( PaStream * stream, void * buffer, long frames )
  36. Callback • Callback when more data is needed. • Non-Blocking

    • PaError Pa_OpenStream (PaStream ** stream, ... , double sampleRate, long framesPerBuffer, ... ,PaStreamCallback * streamCallback, void * userData) • typedef int PaStreamCallback(const void *input, void *output, unsigned long frameCount, ... , void *userData)
  37. Read-Write Example - C // From http://portaudio.com/docs/v19-doxydocs/blocking_read_write.html err = Pa_Initialize();

    if( err != paNoError ) goto error; /* --SKIPPED-- -- Initalize Variables -- --SKIPPED-- */ /* -- setup stream -- */ err = Pa_OpenStream( &stream, &inputParameters,&outputParameters, SAMPLE_RATE,FRAMES_PER_BUFFER, paClipOff, NULL, /* no callback, use blocking API */ NULL ); /* no callback, so no callback userData */ /* -- start stream -- */ err = Pa_StartStream( stream ); printf("Wire on. Will run one minute.\n"); fflush(stdout);
  38. Read-Write Example - C /* -- Here's the loop where

    we pass data from input to output -- */ for( i=0; i<(60*SAMPLE_RATE)/FRAMES_PER_BUFFER; ++i ) { err = Pa_WriteStream( stream, sampleBlock, FRAMES_PER_BUFFER ); err = Pa_ReadStream( stream, sampleBlock, FRAMES_PER_BUFFER ); } /* -- Now we stop the stream -- */ err = Pa_StopStream( stream ); /* -- don't forget to cleanup! -- */ err = Pa_CloseStream( stream ); Pa_Terminate(); return 0; }
  39. Callback Example - C // http://portaudio.com/docs/v19-doxydocs/paex__sine_8c_source.html err = Pa_Initialize(); if(

    err != paNoError ) goto error; err = Pa_OpenStream( &stream,NULL,&outputParameters, SAMPLE_RATE, FRAMES_PER_BUFFER, paClipOff, patestCallback, &data ); if( err != paNoError ) goto error; printf("Setup Done\n"); err = Pa_SetStreamFinishedCallback( stream, &StreamFinished ); err = Pa_StartStream( stream ); printf("Play for %d seconds.\n", NUM_SECONDS ); Pa_Sleep( NUM_SECONDS * 1000 ); err = Pa_StopStream( stream ); err = Pa_CloseStream( stream ); Pa_Terminate(); return err;
  40. Callback Example - C static int patestCallback( const void *inputBuffer,

    void *outputBuffer, unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo* timeInfo, PaStreamCallbackFlags statusFlags, void *userData ) { for( i=0; i<framesPerBuffer; i++ ){ *out++ = data->sine[data->left_phase]; /* left */ *out++ = data->sine[data->right_phase]; /* right */ data->left_phase += 1; if( data->left_phase >= TABLE_SIZE ) data->left_phase -= TABLE_SIZE; data->right_phase += 3; if( data->right_phase >= TABLE_SIZE ) data->right_phase -= TABLE_SIZE; } return paContinue; }
  41. Callback Example - AS3 var mySound:Sound = new Sound(); function

    sineWaveGenerator(event:SampleDataEvent):void { for ( var c:int=0; c<8192; c++ ) { event.data.writeFloat(Math.sin((Number(c+event. position)/Math.PI/2))*0.25); event.data.writeFloat(Math.sin((Number(c+event. position)/Math.PI/2))*0.25); } } mySound.addEventListener(SampleDataEvent.SAMPLE_DATA, sineWaveGenerator); mySound.play();
  42. Hands On