Core Audio Essentials – Sound Files and Sound Streams

Tram Ho

Sound Files

To work with sound files in the application, we can use Audio File Services, one of Core Audio’s mid-level services. Audio File Services provides a powerful ability to access audio data, metadata, and to create sound files.

In addition to allowing us to work with basic information such as file ID, file type and data format, Audio File Services allows us to work with regions, markers, iterations, directions, SMPTE time code, etc. ..

We also use Audio File Services to check the features of the system. Some typical functions that we use are AudioFileGetGlobalInfoSize (get size to allocate memory for the information we want to get) and AudioFileGetGlobalInfo (to get information). A long list of properties included in the AudioFile.h file allows you to test system features such as:

  • Types of files that can be read.
  • Types of writable files.
  • For each type of recording, what is the audio format that we can put into the file.

This article will also introduce you to Core Audio’s two technologies:

  • Audio File Stream Services, an audio stream parsing interface, allows us to read audio data from a drive or from a network stream.
  • Extended Audio File Services (Mac only) encapsulates features from Audio File Services and Audio Converter Services, making our code simpler.

Create a new sound file

To create a sound file to record into, we need:

  • The path to the file, as CFURL or NSURL .
  • The identifier of the type of file we want to create is described in the Audio File Types enum inside AudioFile.h . For example, to create a CAF file, we use the identifier as kAudioFileCAFType .
  • Audio stream basic description (ASBD) for the audio data that we will include in the file. In many cases, you can provide an ASBD and ask Audio File Services to complete it manually.

We pass these three information to the Audio File Services via the parameters of the AudioFileCreateWithURL function, which creates the file and returns an AudioFileID object. We use this object for future interactions with the sound file.

Open a sound file

To open a sound file for playback, use the AudioFileOpenURL function. We provide this function with the URL of the file, a constant that suggests the file type, and the file access permissions we want to use. AudioFileOpenURL will return the ID of the file.

We then use the identifier property along with the AudioFileGetPropertyInfo , AudioFileGetProperty functions to get the necessary information from the file. Some commonly used property identifiers and names look easy to understand:

  • kAudioFilePropertyFileFormat
  • kAudioFilePropertyDataFormat
  • kAudioFilePropertyMagicCookieData
  • kAudioFilePropertyChannelLayout

There are many other identifiers in Audio File Services that allow us to receive possible metadata in the file, such as markers, copyright information, play rate.

When encountering a very long VBR file, such as a podcast, receiving entire packets tables can consume significant amounts of time. In such a case, the following two identifier properties are particularly useful: kAudioFilePropertyPacketSizeUpperBound and kAudioFilePropertyEstimatedDuration . We can use these to quickly calculate the approximate sound file length or number of packets, instead of analyzing the entire file to get the exact number.

Read and write a sound file

In iOS we often use Audio File Services to read and write audio data with sound files. Reading and writing are basically two opposing images when we use Audio File Services. Both processes block the current thread until completion, and both can operate using bytes or packets. However, unless we have some special requirements, please use the packet.

  • Reading and writing by packet is the only option for VBR data.
  • Using packet-based operation helps us calculate audio duration more easily.

Another option in iOS to read audio data from disk is Audio File Stream Services. At the end of the article, I will discuss this later.

Audo Queue Services, declared in AudioQueue.h in the Audio Toolbox framework, is a Core Audio interface for recording and playback. To get an overview of Audio Queue Services, keep track of the next post.

Extended Audio File Services

Core Audio provides a handy API called Extended Audio File Services. This interface includes the necessary functions in Audio File Services and Audio Converter Services, providing automatic conversion of linear PCM format. We will have another article to clarify this issue.

iPhone Audio File Formats

iOS supports audio file format listed in the table below. And for information about audio data formats in iOS, keep track of the next post.

Format nameFormat filename extensions
AIFF.aif, .aiff
CAF.caf
MPEG-1, layer 3.mp3
MPEG-2 or MPEG-4 ADTS.aac
MPEG-4.m4a, mp4
WAV.WAV
AC-3 (Dolby Digital).ac3
Enhanced AC-3 (Dolby Digital Plus).ec3

CAF Files

iOS and OS X have a native audio file format called Core Audio Format (or CAF). CAF format was introduced in OS X v10.4 “Tiger” and is available from iOS 2.0 and above. The unique thing here is that it can hold any audio data format supported on the platform.

CAF files do not have size limits, unlike AIFF and WAV, and can support many types of metadata, but channel information and text annotations. For more information about this format, you can refer to Apple Core Audio Format Specification 1.0 .

Sound Streams

Unlike a disk-based sound file, an audio file stream is an audio data that you may not have access to. What we interact with is a stream. For example, when we build an Internet radio player app. Providers usually send their stream continuously. When the user presses Play to listen, the application needs to jump to whatever data is being used at the time. The beginning, middle or end of an audio packet, or a magic cookie.

And unlike sound files, the data of a stream may not be a reliable form of data. Packets may be dropped, discontinuity, or paused, depending on the network problem.

Audio File Stream Services allows the application to work with streams and all that complexity. It takes care of parsing.

To use Audio File Stream Services, we create an audio file stream object, in the form of AudioFileStreamID . This object is used as a proxy for streaming. This object also lets your application know what is happening with the stream through properties. For example, when Audio File Stream Services determines the bitrate of the stream, it sets the kAudioFileStreamProperty_BitRate property in the audio file stream object.

Because Audio File Stream Services does the parsing, it becomes a role of the app in response to the provision of audio data files and other information. We use two callback functions to do this:

First, we need a callback for changes in the audio file stream object. At a minimum, we need to write this callback to respond to changes in the kAudioFileStreamProperty_ReadyToProducePackets property. The typical scenario when using this property is as follows:

  1. A user clicks Play, or other interactions require the stream to begin playing.
  2. Audio File Stream Services starts parse the stream.
  3. When enough audio data packets are parse to send throughout the app, Audio File Stream Services sets the kAudioFileStreamProperty_ReadyToProducePackets property to true (actually value 1) in our audio file stream object.
  4. Audio File Stream Services invoke callbacks, along with the value of the kAudioFileStreamProperty_ReadyToProducePackets property.
  5. The callback function will select the appropriate action, such as setting up an audio queue object to stream.

Second, we need a callback for audio data. Audio File Stream Services calls this callback function whenever it collects a complete set of audio data packets. We set these callbacks to process the received audio. Usually, we broadcast it immediately by sending it to Audio Queue Services. For more details about playback, please look forward to the following articles.

Conclusion

Above are some issues related to Sound Files and Sound Stream in Core Audio, hoping to bring a source of reference knowledge for everyone.


Translations and references from Core Audio Essentials

Share the news now

Source : Viblo