Playing & Recording Audio Audio Queue Services Playback

Similar documents
Playing & Recording Audio Audio Queue Services Playback - AQPlayer Class

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

Audio Queue Services. Dave

Audio Unit Properties Reference

Short Notes of CS201

CS201 - Introduction to Programming Glossary By

CS349/SE382 A1 C Programming Tutorial

Quiz 0 Review Session. October 13th, 2014

#include <tobii/tobii.h> char const* tobii_error_message( tobii_error_t error );

Common Misunderstandings from Exam 1 Material

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

PRINCIPLES OF OPERATING SYSTEMS

Pointers, Dynamic Data, and Reference Types

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

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

PusleIR Multitouch Screen Software SDK Specification. Revision 4.0

CS 241 Data Organization Binary Trees

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

(6-1) Basics of a Queue. Instructor - Andrew S. O Fallon CptS 122 (September 26, 2018) Washington State University

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

SpiNNaker Application Programming Interface (API)

CS2141 Software Development using C/C++ C++ Basics

Crit-bit Trees. Adam Langley (Version )

Character Strings. String-copy Example

C#: framework overview and in-the-small features

Opus Generated by Doxygen Thu May :22:05

Anybus CompactCom. Host Application Implementation Guide. Doc.Id. HMSI Doc. Rev Connecting DevicesTM

CS 140 Project 4 File Systems Review Session

CAAM 420 Daily Note. Scriber: Qijia Jiang. Date: Oct.16. Project 3 Due Wed 23.Oct. Two parts: debug code and library exercise.

Dotstack Porting Guide.

CS Spring 05 - MidTerm

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Trace without a Trace Port on MSC8156 and MSC8144 DSPs

µroar Manual Philipp ph3-der-loewe Schafft

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Technical Report on further interoperability with C

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

_äìéi~ä» Implementing Streams in BlueLab. User Guide. November CSR Cambridge Science Park Milton Road Cambridge CB4 0WH United Kingdom

Kurt Schmidt. October 30, 2018

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS201 Some Important Definitions

Introduce C# as Object Oriented programming language. Explain, tokens,

Mac OS X Server Developer s Kit. Apple Filing Protocol Client

EMBEDDED SYSTEMS PROGRAMMING Language Basics

Introduction to C# Applications

File Systems: Consistency Issues

Understand Computer Storage and Data Types

SIMATIC Industrial software Readme SIMATIC S7-PLCSIM Advanced V2.0 SP1 Readme

Motivation was to facilitate development of systems software, especially OS development.

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Call DLL from Limnor Applications

The XIM Transport Specification

Chapter 2. Procedural Programming

// Initially NULL, points to the dynamically allocated array of bytes. uint8_t *data;

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Dynamic memory. EECS 211 Winter 2019

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CS-211 Fall 2017 Test 2 Version A November 29, Name:

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Embedded Systems Programming

a data type is Types

ATOMS. 3.1 Interface. The Atom interface is simple: atom.h #ifndef ATOM_INCLUDED #define ATOM_INCLUDED

Low-Level C Programming. Memory map Pointers Arrays Structures

Lecture 8 Dynamic Memory Allocation

Scuola Superiore Sant Anna. I/O subsystem. Giuseppe Lipari

Types, Operators and Expressions

September 10,

Crit-bit Trees. Adam Langley (Version )

M/s. Managing distributed workloads. Language Reference Manual. Miranda Li (mjl2206) Benjamin Hanser (bwh2124) Mengdi Lin (ml3567)

6.096 Introduction to C++ January (IAP) 2009

Study Guide for Test 2

Pointers, Pointers, Pointers!

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Motivation was to facilitate development of systems software, especially OS development.

In Java we have the keyword null, which is the value of an uninitialized reference type

DAY 3. CS3600, Northeastern University. Alan Mislove

Heap Arrays and Linked Lists. Steven R. Bagley

Chapter 6. Data Structure. /* start contains the address of the first node */ start = &elephant1; print_elephants( start ); return EXIT_SUCCESS;

Pointers (1A) Young Won Lim 11/1/17

DSP/BIOS LINK. Configurable TSK and SWI approach LNK 207 DES. Version <1.00>

CSE351 Winter 2016, Final Examination March 16, 2016

NOTE: Debug and DebugSingle are the only MPI library configurations that will produce trace output.

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

High Performance Computing in C and C++

Structured Data. CIS 15 : Spring 2007

CS240: Programming in C

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CP2 Revision. theme: dynamic datatypes & data structures

Armide Documentation. Release Kyle Mayes

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Dynamic Memory: Alignment and Fragmentation

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Data Types Literals, Variables & Constants

Cisco TSP Media Driver

Cisco TSP Media Driver

CSCI 171 Chapter Outlines

Transcription:

Playing & Recording Audio Audio Queue Services Playback The most common scenario : basic playing back an on-disk file 11 2010-2 iphone Application 1. 2. Playback Audio Queue Callback 1. Playback Audio Queue Callback 2. 3. 4. 5. Playback Audio Queue Callback 3. Playback Audio Queue Buffer Size 4. Open 1. Obtaining a CFURL Object for an Audio File 2. Opening an Audio File 3. Obtaining a File s Audio Data Format 5. Playback Audio Queue 6. Playback Audio Queue 1. Setting Buffer Size and Number of Packets to Read 2. Allocating Memory for a Packet Descriptions Array 7. Set a Magic Cookie for an Audio File 8. Allocate and Prime Audio Queue Buffers 9. Set an Audio Queue s Playback Gain 10. Start and Run an Audio Queue

iphone Application static const int knumberbuffers = 3; // 1 struct AQPlayerState { AudioStreamBasicDescription mdataformat; // 2 AudioQueueRef mqueue; // 3 AudioQueueBufferRef mbuffers[knumberbuffers]; // 4 AudioFileID maudiofile; // 5 UInt32 bufferbytesize; // 6 SInt64 mcurrentpacket; // 7 UInt32 mnumpacketstoread; // 8 AudioStreamPacketDescription *mpacketdescs; // 9 bool misrunning; // 10 ; 1.. 2.. mqueue. 3.. 4.. 5.. 6. ( ). DeriveBufferSize. 7. ( ). 8.. 9. VBR,. CBR NULL. 10. Boolean. Playback iphone Audio Application Queue Callback Playback Audio Queue Callback - Declaration for a playback audio queue callback function (declared as AudioQueueOutputCallback in the AudioQueue.h header file) static void HandleOutputBuffer ( void *aqdata, // 1 AudioQueueRef inaq, // 2 AudioQueueBufferRef inbuffer // 3 ) 1. aqdata. 2.. 3. inbuffer ( ).

Playback iphone Audio Application Queue Callback ( ) - playback audio queue callback. (declared as AudioFileReadPackets function from the AudioFile.h header file) AudioFileReadPackets ( // 1 paqdata->maudiofile, // 2 false, // 3 &numbytesreadfromfile, // 4 paqdata->mpacketdescs, // 5 paqdata->mcurrentpacket, // 6 &numpackets, // 7 inbuffer->maudiodata // 8 1. AudioFile.h header file AudioFileReadPackets. 2.. 3. false :.. 4. On output,. 5. On output,. CBR data NULL. 6.. 7. On input,. On output,. 8. On output,. Write iphone a Playback Application Audio Queue Callback Audio Queue Buffer -. AudioQueueEnqueueBuffer ( // 1 paqdata->mqueue, // 2 inbuffer, // 3 (paqdata->mpacketdescs? numpackets : 0), // 4 paqdata->mpacketdescs // 5 1. AudioQueueEnqueueBuffer : adds an audio queue buffer to a buffer queue. 2.. 3. 4.. CBR data packet description 0. 5. packet description.

Write iphone a Playback Application Audio Queue Callback Audio Queue -. if (numpackets == 0) { // 1 AudioQueueStop ( // 2 paqdata->mqueue, // 3 false // 4 paqdata->misrunning = false; // 5 1. AudioFileReadPackets 0. 2. AudioQueueStop : audio queue. 3. audio queue. 4. audio queue (asynchronously).. 5.. Write iphone a Playback Application Audio Queue Callback Playback Audio Queue Callback static void HandleOutputBuffer ( void *aqdata, AudioQueueRef inaq, AudioQueueBufferRef inbuffer ) { AQPlayerState *paqdata = (AQPlayerState *) aqdata; // 1 if (paqdata->misrunning == 0) return; // 2 UInt32 numbytesreadfromfile; // 3 UInt32 numpackets = paqdata->mnumpacketstoread; // 4 AudioFileReadPackets ( paqdata->maudiofile, false, &numbytesreadfromfile, paqdata->mpacketdescs, paqdata->mcurrentpacket, &numpackets, inbuffer->maudiodata if (numpackets > 0) { // 5 inbuffer->maudiodatabytesize = numbytesreadfromfile; // 6 AudioQueueEnqueueBuffer ( paqdata->mqueue, inbuffer, (paqdata->mpacketdescs? numpackets : 0), paqdata->mpacketdescs paqdata->mcurrentpacket += numpackets; // 7 else { AudioQueueStop ( paqdata->mqueue, false paqdata->misrunning = false; 1.. (AudioFileID ). 2. audio queue. 3.. 4. numpackets. 5.. enqueue. audio queue. 6. audio queue buffer. 7. packet index.

Playback iphone Audio Queue Application Buffer Size To specify a size for the audio queue buffers void DeriveBufferSize ( AudioStreamBasicDescription &ASBDesc, // 1 UInt32 maxpacketsize, // 2 Float64 seconds, // 3 UInt32 *outbuffersize, // 4 UInt32 *outnumpacketstoread // 5 ) { static const int maxbuffersize = 0x50000; // 6 static const int minbuffersize = 0x4000; // 7 if (ASBDesc.mFramesPerPacket = 0) { // 8 Float64 numpacketsfortime = ASBDesc.mSampleRate / ASBDesc.mFramesPerPacket * seconds; *outbuffersize = numpacketsfortime * maxpacketsize; else { // 9 *outbuffersize = maxbuffersize > maxpacketsize? maxbuffersize : maxpacketsize; if ( // 10 *outbuffersize > maxbuffersize && *outbuffersize > maxpacketsize ) *outbuffersize = maxbuffersize; else { // 11 if (*outbuffersize < minbuffersize) *outbuffersize = minbuffersize; *outnumpacketstoread = *outbuffersize / maxpacketsize; // 12 1. AudioStreamBasicDescription 2.. 3.. 4. On output,. 5. On output, playback audio queue callback. 6. upper bound(byte ). 320 KB, 96kHz 24 bit 5. 7. lower bound(byte ). 16 KB. 8... 9., upper bound audio queue buffer size. 10. upper bound upper bound.. 11. lower bound lower bound. 12.. iphone Application Obtaining a CFURL Object for an Audio File - CFURL. CFURL. CFURLRef audiofileurl = CFURLCreateFromFileSystemRepresentation ( // 1 NULL, // 2 (const UInt8 *) filepath, // 3 strlen (filepath), // 4 false // 5 1. CFURLCreateFromFileSystemRepresentation : CFURL.h header file, filepath CFURL. 2. Uses NULL (or kcfallocatordefault) to use the current default memory allocator. 3. CFURL. filepath. 4. filepath. 5. false : filepath.

iphone Application ( ) Opening an Audio File AQPlayerState aqdata; // 1 OSStatus result = AudioFileOpenURL ( // 2 audiofileurl, // 3 fsrdperm, // 4 0, // 5 &aqdata.maudiofile // 6 CFRelease (audiofileurl // 7 1. AQPlayerState. (AudioFileID ). 2. AudioFileOpenURL : AudioFile.h header file. 3. URL. 4... File Access Permission Constants enumeration. 5. An optional file type hint. 0. 6. On output,. maudiofile. 7. CFURL. iphone Application ( ) Obtaining a File s Audio Data Format UInt32 dataformatsize = sizeof (aqdata.mdataformat // 1 AudioFileGetProperty ( // 2 aqdata.maudiofile, // 3 kaudiofilepropertydataformat, // 4 &dataformatsize, // 5 &aqdata.mdataformat // 6 1.. 2. AudioFileGetProperty : AudioFile.h header file,. 3. AudioFileID.. 4. property ID. 5. On input, AudioStreamBasicDescription.. On output,. 6. On output, AudioStreamBasicDescription..

iphone Playback Application Audio Queue Creating a playback audio queue - AudioQueueNewOutput :,. AudioQueueNewOutput ( // 1 &aqdata.mdataformat, // 2 HandleOutputBuffer, // 3 &aqdata, // 4 CFRunLoopGetCurrent(), // 5 kcfrunloopcommonmodes, // 6 0, // 7 &aqdata.mqueue // 8 1. AudioQueueNewOutput :. 2.. 3.. 4.. 5... 6. run loop mode. Normally, use the kcfrunloopcommonmodes constant. 7. Reserved. Must be 0. 8. On output,. Playback Audio iphone Queue Application Setting Buffer Size and Number of Packets to Read -, UInt32 maxpacketsize; UInt32 propertysize = sizeof (maxpacketsize AudioFileGetProperty ( // 1 aqdata.maudiofile, // 2 kaudiofilepropertypacketsizeupperbound,// 3 &propertysize, // 4 &maxpacketsize // 5 DeriveBufferSize ( // 6 aqdata.mdataformat, // 7 maxpacketsize, // 8 0.5, // 9 &aqdata.bufferbytesize, // 10 &aqdata.mnumpacketstoread // 11 1. AudioFileGetProperty :. 2. (AudioFileID ) 3. upper bound ID. 4. On output,. 5. On output, upper bound.. 6. DeriveBufferSize :,. 7.. 8. 5. 9. ( ). 0.5. 10.On output, ( ).. 11.On output,..

Playback Audio iphone Queue Application Allocating Memory for a Packet Descriptions Array - VBR.. bool isformatvbr = ( // 1 aqdata.mdataformat.mbytesperpacket == 0 aqdata.mdataformat.mframesperpacket == 0 if (isformatvbr) { // 2 aqdata.mpacketdescs = (AudioStreamPacketDescription*) malloc ( aqdata.mnumpacketstoread * sizeof (AudioStreamPacketDescription) else { // 3 aqdata.mpacketdescs = NULL; 1. VBR CBR. VBR bytes-per-packet framesper-packet 0. AudioStreamBasicDescription. 2. VBR.. 3. PCM CBR. iphone Set a Magic Application Cookie for an Audio File. UInt32 cookiesize = sizeof (UInt32 // 1 bool couldnotgetproperty = // 2 AudioFileGetPropertyInfo ( // 3 aqdata.maudiofile, // 4 kaudiofilepropertymagiccookiedata, // 5 &cookiesize, // 6 NULL // 7 if (couldnotgetproperty && cookiesize) { // 8 char* magiccookie = (char *) malloc (cookiesize AudioFileGetProperty ( // 9 aqdata.maudiofile, // 10 kaudiofilepropertymagiccookiedata, // 11 &cookiesize, // 12 magiccookie // 13 AudioQueueSetProperty ( // 14 aqdata.mqueue, // 15 kaudioqueueproperty_magiccookie, // 16 magiccookie, // 17 cookiesize // 18 free (magiccookie // 19 1.. 3. AudioFileGetPropertyInfo : AudioFile.h header file.. 5. The property ID representing an audio file s magic cookie data. 6. On input,. On output,. 7. Uses NULL to indicate that you don t care about the read/write access for the property. 8.,.. 9. AudioFileGetProperty : AudioFile.h header file...

iphone Set a Magic Application Cookie for an Audio File UInt32 cookiesize = sizeof (UInt32 // 1 bool couldnotgetproperty = // 2 AudioFileGetPropertyInfo ( // 3 aqdata.maudiofile, // 4 kaudiofilepropertymagiccookiedata, // 5 &cookiesize, // 6 NULL // 7 if (couldnotgetproperty && cookiesize) { // 8 char* magiccookie = (char *) malloc (cookiesize AudioFileGetProperty ( // 9 aqdata.maudiofile, // 10 kaudiofilepropertymagiccookiedata, // 11 &cookiesize, // 12 magiccookie // 13 AudioQueueSetProperty ( // 14 aqdata.mqueue, // 15 kaudioqueueproperty_magiccookie, // 16 magiccookie, // 17 cookiesize // 18 free (magiccookie // 19 13. On output, the audio file s magic cookie. 14. AudioQueueSetProperty :.. Allocate iphone and Application Prime Audio Queue Buffers Allocating and priming audio queue buffers for playback -,. aqdata.mcurrentpacket = 0; // 1 for (int i = 0; i < knumberbuffers; ++i) { // 2 AudioQueueAllocateBuffer ( // 3 aqdata.mqueue, // 4 aqdata.bufferbytesize, // 5 &aqdata.mbuffers[i] // 6 HandleOutputBuffer ( // 7 &aqdata, // 8 aqdata.mqueue, // 9 aqdata.mbuffers[i] // 10 1. Sets the packet index to 0, so that when the audio queue callback starts filling buffers (step 7) it starts at the beginning of the audio file. 2. Allocates and primes a set of audio queue buffers. 3. The AudioQueueAllocateBuffer function creates an audio queue buffer by allocating memory for it. 4. The audio queue that is allocating the audio queue buffer. 5. The size, in bytes, for the new audio queue buffer. 6. On output, adds the new audio queue buffer to the mbuffers array in the custom structure. 7. The HandleOutputBuffer function is the playback audio queue callback you wrote. 8. The custom structure for the audio queue. 9. The audio queue whose callback you re invoking. 10.The audio queue buffer that you re passing to the audio queue callback.

iphone Set an Audio Application Queue s Playback Gain, audio queue parameter mechanism. Float32 gain = 1.0; // 1 // Optionally, allow user to override gain setting here AudioQueueSetParameter ( // 2 aqdata.mqueue, // 3 kaudioqueueparam_volume, // 4 gain // 5 1. Sets a gain to use with the audio queue, between 0 (for silence) and 1 (for unity gain). 2. The AudioQueueSetParameter function sets the value of a parameter for an audio queue. 3. The audio queue that you are setting a parameter on. 4. The ID of the parameter you are setting. The kaudioqueueparam_volume constant lets you set an audio queue s gain. 5. The gain setting that you are applying to the audio queue. iphone Start and Application Run and Audio Queue., run loop. aqdata.misrunning = true; // 1 AudioQueueStart ( // 2 aqdata.mqueue, // 3 NULL // 4 do { // 5 CFRunLoopRunInMode ( // 6 kcfrunloopdefaultmode, // 7 0.25, // 8 false // 9 while (aqdata.misrunning CFRunLoopRunInMode ( // 10 kcfrunloopdefaultmode, 1, false 1. Sets a flag in the custom structure to indicate that the audio queue is running. 2. The AudioQueueStart function starts the audio queue, on its own thread. 3. The audio queue to start. 4. Uses NULL to indicate that the audio queue should start playing immediately. 5. Polls the custom structure s misrunning field regularly to check if the audio queue has stopped. 6. CFRunLoopRunInMode : run loop. 7. Uses the default mode for the run loop. 8. run loop 0.25. 9. false : run loop 0.25. 10. run loop.

iphone Clean Application Up After Playing When you re finished with recording, dispose of the audio queue and close the audio file. AudioQueueDispose ( // 1 aqdata.mqueue, // 2 true // 3 AudioFileClose (aqdata.maudiofile // 4 free (aqdata.mpacketdescs // 5 1. The AudioQueueDispose function disposes of the audio queue and all of its resources, including its buffers. 2. The audio queue you want to dispose of. 3. Use true to dispose of the audio queue synchronously. 4. Closes the audio file that was played. The AudioFileClose function is declared in the AudioFile.h header file. 5. Releases the memory that was used to hold the packet descriptions.