Audio Queue Services. Dave

Similar documents
Playing & Recording Audio Audio Queue Services Recording - AQRecorder Class

Contents. Introduction 7. What Is Core Audio? 10. Core Audio Essentials 19. Organization of This Document 8 See Also 8

Playing & Recording Audio Audio Queue Services Playback - AQPlayer Class

What s New in Core Audio

Apple Core Audio Format Specification 1.0

Playing & Recording Audio Audio Queue Services Playback

ITP 342 Mobile App Dev. Audio

For Mac and iphone. James McCartney Core Audio Engineer. Eric Allamanche Core Audio Engineer

ITP 342 Mobile App Dev. Audio

Fundamental of Digital Media Design. Introduction to Audio

the gamedesigninitiative at cornell university Lecture 15 Game Audio

Digital Audio Basics

Skill Area 214: Use a Multimedia Software. Software Application (SWA)

What s New in Audio. Media #WWDC17. Akshatha Nagesh, AudioEngine-eer Béla Balázs, Audio Artisan Torrey Holbrook Walker, Audio/MIDI Black Ops

Reference8 Pro music player Fast Operation Guide

Computer Audio Setup Guide

UNDERSTANDING MUSIC & VIDEO FORMATS

QuickTime 4 Reference. For Macintosh and Windows

Compressed Audio Demystified by Hendrik Gideonse and Connor Smith. All Rights Reserved.

Audio Unit Properties Reference

Design Brief CD15 Prisma Compact Disc and Digital Music Player

08 Sound. Multimedia Systems. Nature of Sound, Store Audio, Sound Editing, MIDI

2.1 Transcoding audio files

Super UA Q&A 2015/03/16

Export Audio Mixdown

3.01C Multimedia Elements and Guidelines Explore multimedia systems, elements and presentations.

Audio for Everybody. OCPUG/PATACS 21 January Tom Gutnick. Copyright by Tom Gutnick. All rights reserved.

CHAPTER 6 Audio compression in practice

MARANTZ GUIDE TO PC-AUDIO

Design Brief CD35 Prisma Compact Disc and Digital Music Player

ITEC 120 2/25/11. Review. Objectives. Sound. Arrays. Lecture 18 Sound. See a real world usage of arrays

INSTALLATION MBL USB LINK MCMI

TEAC HR Audio Player. Music Playback Software for TEAC USB AUDIO DAC Devices OWNER S MANUAL

Multi-Room Music Servers

Audio issues in MIR evaluation

CS 074 The Digital World. Digital Audio

AudioGate version Release Information (Windows)

ITNP80: Multimedia! Sound-II!

Symphony By Numbers. Symphony By Numbers is a work of Visual Music. Visual Music can be

Announcements. Today s Topics

VS1063 ENCODER DEMONSTRATION

Delivering an Exceptional Audio Experience

UM1641 User manual. Sampling rate conversion SRC236 library software expansion for STM32Cube. Introduction

Optimizing Audio for Mobile Development. Ben Houge Berklee College of Music Music Technology Innovation Valencia, Spain

TEAC HR Audio Player. Music Playback Software for TEAC USB AUDIO DAC Devices OWNER S MANUAL

Standard Audio Device Driver Specification

Squeeze Play: The State of Ady0 Cmprshn. Scott Selfon Senior Development Lead Xbox Advanced Technology Group Microsoft

Audio Coding and MP3

Lossy compression CSCI 470: Web Science Keith Vertanen Copyright 2013

ADDING MUSIC TO YOUR itunes LIBRARY

PDF Document structure, that need for managing of PDF file. It uses in all functions from EMF2PDF SDK.

µroar Manual Philipp ph3-der-loewe Schafft

USB and DSD Setup Guide. Audiolab USB and DSD Setup Guide :29:24

Media Formats. Sound. Image. Video. WAV (uncompressed, PCM) MIDI. BMP XML dia MPEG

OLIVE 4 & 4HD HI-FI MUSIC SERVER P R O D U C T O V E R V I E W

PCM-A10. High-Resolution Digital Audio recorder with 3-way adjustable microphones. Overview. Features

Elementary Computing CSC 100. M. Cheng, Computer Science

CS193P - Lecture 16. iphone Application Development. Audio APIs Video Playback Displaying Web Content Settings

elastique efficient time stretching SDK by zplane.development (c) 2017 zplane.development GmbH & Co. KG

ESOTERIC HR Audio Player

RETAIL Price List - Valid From June 2018

Contents. Player Camera AudioIn, AudioOut Image & ImageUtil AudioRecorder VideoRecorder AudioDecoder/Encoder, VideoDecoder/Encoder

ARM MPEG-4 AAC LC Decoder Technical Specification

AAMS Auto Audio Mastering System V3 Manual

Opus Generated by Doxygen Thu May :22:05

Distributed Information Processing

Design Brief SC15 Prisma

Lossy compression. CSCI 470: Web Science Keith Vertanen

BEOSOUND CORE THE HEART OF YOUR MUSIC

Using and Extending the Xcode Source Editor

Computing in the Modern World

Digital Media. Daniel Fuller ITEC 2110

Media Playback and Recording. CS193W - Spring Lecture 3

RC-1590 Stereo Preamplifier PRELIMINARY

Using the Camera with AV Foundation Overview and best practices. Brad Ford iphone Engineering

SoundBridge Helpful Tips. For customers who want to use Roku SoundBridge with the SlimServer music server

Binary representation and data

BCS Autumn School. Distinguish your application with Bonjour. Nan Xu. 20 November 2013

3 Sound / Audio. CS 5513 Multimedia Systems Spring 2009 LECTURE. Imran Ihsan Principal Design Consultant

Design Brief I15 Prisma Integrated Amplifier

int fnvgetconfig(handle h, UINT32 id, const void *cfg, size_t sz);... 4

AAC-ELD based Audio Communication on ios A Developer s Guide

ENHANCEMENT IMPLEMENTATION ON

AVAudioRecorder & System Sound Services

Lecture #3: Digital Music and Sound

Core Audio. MSDOSX : Lecture 20

Mark the beginning and ending of a particular recorded passage and the recording will be played over and over again- great for language learning.

NEW PRODUCTS FALL 2017

Embedding Audio into your RX Application

Audio Studio Guide Version: 3.5 Author: Ton Valkenburgh Date: January 8,

UPDATES AND CORRECTIONS JANUARY 2002

Introducing working with sounds in Audacity

INSTRUCTIONS FOR USE Pro-Ject Phono Box DS2 USB

VISUAL QUICKSTART GUIDE QUICKTIME PRO 4. Judith Stern Robert Lettieri. Peachpit Press

Smart Pianist FAQ. Availability of certain functions differs depending on the connected instrument.

Audio Unit Extensions

Sometimes dreams come true

Smart Pianist FAQ. Availability of certain functions differs depending on the connected instrument.

Lecture 16 Perceptual Audio Coding

Be sure you have Audacity AND the LAME Encoder installed. Both are available in the Software Installation Center.

Transcription:

Audio Queue Services Dave Dribin @ddribin

Playing Audio on iphone OS

Playing Audio on iphone OS Media Player Framework

Playing Audio on iphone OS Media Player Framework AV Foundation Framework

Playing Audio on iphone OS Media Player Framework AV Foundation Framework OpenAL Library

Playing Audio on iphone OS Media Player Framework AV Foundation Framework OpenAL Library Core Audio Framework

Playing Audio on iphone OS Media Player Framework AV Foundation Framework OpenAL Library Core Audio Framework Audio Toolbox Framework

Playing Audio on iphone OS Media Player Framework AV Foundation Framework OpenAL Library Core Audio Framework Audio Toolbox Framework Audio Unit Framework

Playing Audio on iphone OS Media Player Framework AV Foundation Framework OpenAL Library Audio Toolbox Framework Audio Unit Framework

Final Application Demo

Crash Course in Digital Audio

Analog Audio - A.K.A Sound 1.5 1 0.5 0 0.8 1.6 2.4 3.2 4 4.8 5.6 6.4 7.2 8-0.5-1 -1.5

Analog Audio - A.K.A Sound 1.5 1 0.5 0 0.8 1.6 2.4 3.2 4 4.8 5.6 6.4 7.2 8-0.5-1 -1.5

Digital Audio

Digital Audio Glossary Format Example: Linear Pulse Code Modulation (Linear PCM) Sampling Rate Example: 44,100 Hz Sampling Resolution Example: 16-bit signed integer Channels Example: 2 (for stereo)

CD Audio Linear PCM 44.1 khz sampling rate 16-bit signed integer 2 Channels (Stereo)

Digital Telephony µ-law PCM 8 khz Sampling Rate 8-bit unsigned integer 1 channel (monaural)

Audio Compression Lossless WAV, AIFF (1:1) FLAC, Apple Lossless (2:1) Lossy MP3, AAC (10:1)

Audio Queue Services Hello World Play 440 Hz sound A440 Project on BitBucket: http://bitbucket.org/ddribin/a440

Demo

What is an Audio Queue

Creating an Audio Queue for Output OSStatus AudioQueueNewOutput(const AudioStreamBasicDescription AudioQueueOutputCallback void CFRunLoopRef CFStringRef UInt32 AudioQueueRef *informat, incallbackproc, *inuserdata, incallbackrunloop, incallbackrunloopmode, inflags, *outaq);

AudioStreamBasicDescription (ASBDs) struct AudioStreamBasicDescription { Float64 msamplerate; UInt32 mformatid; UInt32 mformatflags; UInt32 mbytesperpacket; UInt32 mframesperpacket; UInt32 mbytesperframe; UInt32 mchannelsperframe; UInt32 mbitsperchannel; UInt32 mreserved; ; typedef struct AudioStreamBasicDescription AudioStreamBasicDescription;

16-bits per Sample, Stereo Byte Stream Left Channel Right Channel 0x83 0x74 0x6F 0x72 0x00 0x2F 0x55 0x61 0x42 0x13 0xDF 0xA2 Byte One Sample One Frame

16-bits per Sample, Stereo Byte Stream Left Channel Right Channel 0x83 0x74 0x6F 0x72 0x00 0x2F 0x55 0x61 0x42 0x13 0xDF 0xA2 Byte One Sample One Frame

16-bits per Sample, Stereo Byte Stream Left Channel Right Channel 0x83 0x74 0x6F 0x72 0x00 0x2F 0x55 0x61 0x42 0x13 0xDF 0xA2 Byte One Sample One Frame

Audio Queue Sine Player @interface A440AudioQueue : NSObject <A440Player> { AudioQueueRef _queue; AudioStreamBasicDescription _dataformat; AudioQueueBufferRef _buffers[3]; A440SineWaveGenerator _sinewavegenerator; BOOL _shouldbufferdataincallback; - (BOOL)play:(NSError **)error; - (BOOL)stop:(NSError **)error; @end

- (BOOL)play:(NSError **)error; { NSAssert(_queue == NULL, @"Queue is already setup"); OSStatus status; [self setupdataformat]; FAIL_ON_ERR(AudioQueueNewOutput(&_dataFormat, HandleOutputBuffer, self, CFRunLoopGetCurrent(), kcfrunloopcommonmodes, 0, &_queue)); FAIL_ON_ERR([self allocatebuffers]); A440SineWaveGeneratorInitWithFrequency(&_sineWaveGenerator, 440.0); [self primebuffers]; FAIL_ON_ERR(AudioQueueStart(_queue, NULL)); return YES; failed: // Error handling... return NO;

- (void)setupdataformat; { // 16-bit native endian signed integer, stereo LPCM UInt32 formatflags = (0 kaudioformatflagispacked kaudioformatflagissignedinteger kaudioformatflagsnativeendian ); _dataformat = (AudioStreamBasicDescription){.mFormatID = kaudioformatlinearpcm,.mformatflags = formatflags,.msamplerate = SAMPLE_RATE,.mBitsPerChannel = 16,.mChannelsPerFrame = 2,.mBytesPerFrame = 4,.mFramesPerPacket = 1,.mBytesPerPacket = 4, ;

- (BOOL)play:(NSError **)error; { NSAssert(_queue == NULL, @"Queue is already setup"); OSStatus status; [self setupdataformat]; FAIL_ON_ERR(AudioQueueNewOutput(&_dataFormat, HandleOutputBuffer, self, CFRunLoopGetCurrent(), kcfrunloopcommonmodes, 0, &_queue)); FAIL_ON_ERR([self allocatebuffers]); A440SineWaveGeneratorInitWithFrequency(&_sineWaveGenerator, 440.0); [self primebuffers]; FAIL_ON_ERR(AudioQueueStart(_queue, NULL)); return YES; failed: // Error handling... return NO;

- (OSStatus)allocateBuffers; { UInt32 buffersize = [self calculatebuffersizeforseconds:0.5]; OSStatus status; for (int i = 0; i < knumberbuffers; ++i) { status = AudioQueueAllocateBuffer(_queue, buffersize, &_buffers[i]); if (status!= noerr) { return status; return noerr;

- (UInt32)calculateBufferSizeForSeconds:(Float64)seconds; { UInt32 buffersize = (_dataformat.msamplerate * _dataformat.mbytesperpacket * seconds); return buffersize;

- (void)primebuffers; { _shouldbufferdataincallback = YES; for (int i = 0; i < knumberbuffers; ++i) { HandleOutputBuffer(self, _queue, _buffers[i]);

- (BOOL)play:(NSError **)error; { NSAssert(_queue == NULL, @"Queue is already setup"); OSStatus status; [self setupdataformat]; FAIL_ON_ERR(AudioQueueNewOutput(&_dataFormat, HandleOutputBuffer, self, CFRunLoopGetCurrent(), kcfrunloopcommonmodes, 0, &_queue)); FAIL_ON_ERR([self allocatebuffers]); A440SineWaveGeneratorInitWithFrequency(&_sineWaveGenerator, 440.0); [self primebuffers]; FAIL_ON_ERR(AudioQueueStart(_queue, NULL)); return YES; failed: // Error handling... return NO;

static void HandleOutputBuffer(void * inuserdata, AudioQueueRef inaq, AudioQueueBufferRef inbuffer) { A440AudioQueue * self = inuserdata; if (!self->_shouldbufferdataincallback) { return; int16_t * sample = inbuffer->maudiodata; UInt32 numberofframes = (inbuffer->maudiodatabytescapacity / self->_dataformat.mbytesperframe); for (UInt32 i = 0; i < numberofframes; i++) { FillFrame(self, sample); sample += self->_dataformat.mchannelsperframe; inbuffer->maudiodatabytesize = (numberofframes * self->_dataformat.mbytesperframe); OSStatus result; result = AudioQueueEnqueueBuffer(self->_queue, inbuffer, 0, NULL); if (result!= noerr) { NSLog(@"AudioQueueEnqueueBuffer error: %d", result);

static void FillFrame(A440AudioQueue * self, int16_t * sample) { A440SineWaveGenerator * generator = &self->_sinewavegenerator; int16_t samplevalue = A440SineWaveGeneratorNextSample(generator); // Divide by four to keep the volume away from the max samplevalue /= 4; // Fill two channels sample[0] = samplevalue; sample[1] = samplevalue;

typedef struct { float currentphase; float phaseincrement; A440SineWaveGenerator; const Float64 SAMPLE_RATE = 44100.0; void A440SineWaveGeneratorInitWithFrequency( A440SineWaveGenerator * self, double frequency) { // Given: // frequency in cycles per second // 2*PI radians per sine wave cycle // sample rate in samples per second // // Then: // cycles radians seconds radians // ------ * ------- * ------- = ------- // second cycle sample sample self->currentphase = 0.0; self->phaseincrement = frequency * 2*M_PI / SAMPLE_RATE;

int16_t A440SineWaveGeneratorNextSample( A440SineWaveGenerator * self) { int16_t sample = INT16_MAX * sinf(self->currentphase); self->currentphase += self->phaseincrement; // Keep the value between 0 and 2*M_PI while (self->currentphase > 2*M_PI) { self->currentphase -= 2*M_PI; return sample;

- (BOOL)stop:(NSError **)error; { NSAssert(_queue!= NULL, @"Queue is not setup"); OSStatus status; _shouldbufferdataincallback = NO; FAIL_ON_ERR(AudioQueueStop(_queue, YES)); FAIL_ON_ERR(AudioQueueDispose(_queue, YES)); _queue = NULL; return YES; failed: // Error handling... return NO;

Audio Session Activation and Deactivation Session Categories Interruption Callbacks Two APIs: AVAudioSession - Objective-C Audio Session Services - C

- (void)setupaudiosession; { [self activateaudiosession]; [[AVAudioSession sharedinstance] setdelegate:self]; [self setplaybackaudiosessioncategory]; - (void)begininterruption; { _playonendinterruption = self.isplaying; [self stop]; - (void)endinterruption; { [self activateaudiosession]; if (_playonendinterruption) { [self play];

Chiptune Player

Nintendo Sound Format (NSF)

Nintendo Sound Format (NSF) Similar to MIDI Two square waves One triangle wave One noise channel One (primitive) sample channel Need to emulate a 6502

@interface GmeMusicFile : NSObject { MusicEmu * _emu; + (id)musicfileatpath:(nsstring *)path samplerate:(long)samplerate error:(nserror **)error; + (id)musicfileatpath:(nsstring *)path error:(nserror **)error; - (long)samplerate; - (int)numberoftracks; - (TrackInfo *)infofortrack:(int)track; - (BOOL)playTrack:(int)track error:(nserror **)error; - (BOOL)trackEnded; BOOL GmeMusicFilePlay(GmeMusicFile * file, long count, short * samples, NSError ** error); @end

static void HandleOutputBuffer(void * inuserdata, AudioQueueRef inaq, AudioQueueBufferRef inbuffer) { MusicPlayerAudioQueueOutput * self = (MusicPlayerAudioQueueOutput *) inuserdata; if (!self->_shouldbufferdataincallback) { return; GmeMusicFile * musicfile = self->_musicfile; if (musicfile == nil) { NSLog(@"No music file"); return;

NSError * error = nil; if (!GmeMusicFilePlay(musicFile, inbuffer->maudiodatabytescapacity/2, inbuffer->maudiodata, &error)) { NSLog(@"GmeMusicFilePlay error: %@ %@", error, [error userinfo]); return; inbuffer->maudiodatabytesize = inbuffer->maudiodatabytescapacity; OSStatus result = AudioQueueEnqueueBuffer(self->_queue, inbuffer, 0, NULL); if (result!= noerr) { NSLog(@"AudioQueueEnqueueBuffer error: %d", result);

// Asynchronous stop means all queued buffers still // get played. if ([musicfile trackended]) { self->_shouldbufferdataincallback = NO; self->_stoppedduetotrackending = YES; AudioQueueStop(self->_queue, NO);

Handling File Ending Asynchronous Stop: AudioQueueStop(_queue, NO); Setup a Property Listener: AudioQueueAddPropertyListener(_queue, kaudioqueueproperty_isrunning, HandleIsRunningChanged, self);

Other Audio Queue Goodies Control the volume Level metering Handle compression without using AudioConverter