translation:adc:audio:audio_queue_services_programming_guide:0300_recordingaudio

差分

このページの2つのバージョン間の差分を表示します。

この比較画面にリンクする

次のリビジョン
前のリビジョン
translation:adc:audio:audio_queue_services_programming_guide:0300_recordingaudio [2014-09-01 13:17]
Decomo 作成
translation:adc:audio:audio_queue_services_programming_guide:0300_recordingaudio [2020-12-04 09:32] (現在)
Decomo
行 1: 行 1:
-<align right>翻訳元:[[https://developer.apple.com/library/mac/documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/AQRecord/RecordingAudio.html|Audio Queue Services Programming Guide: Recording Audio]]</align>+<WRAP rightalign>翻訳元:[[https://developer.apple.com/library/mac/documentation/MusicAudio/Conceptual/AudioQueueProgrammingGuide/AQRecord/RecordingAudio.html|Audio Queue Services Programming Guide: Recording Audio]]</WRAP>
  
 ※以前のwikiで本章は翻訳途中だったため、10.9のリファレンスから再翻訳しています。 ※以前のwikiで本章は翻訳途中だったため、10.9のリファレンスから再翻訳しています。
行 44: 行 44:
  
   - 使用するAudio Queue Bufferの数の設定。   - 使用するAudio Queue Bufferの数の設定。
-  - <ilcode>AudioStreamBasicDescription</ilcode>構造体(<ilcode>CoreAudioType.h</ilcode>より)は、ディスクに書き込む音声データフォーマットを表します。このフォーマットは<ilcode>mQueue</ilcode>フィールドで指定されるAudio Queueによって使用されます。 +  -- ''AudioStreamBasicDescription''構造体(''CoreAudioType.h''より)は、ディスクに書き込む音声データフォーマットを表します。このフォーマットは''mQueue''フィールドで指定されるAudio Queueによって使用されます。 
-  - <ilcode>mDataFormat</ilcode>フィールドは、“[[Set Up an Audio Format for Recording]]”で解説されるように、最初はあなたのプログラムのコードによって埋められます。“[[Getting the Full Audio Format from an Audio Queue]]”で解説するように、Audio Queueに<ilcode>kAudioQueueProperty_StreamDescription</ilcode>プロパティを問い合わせて、このフィールドを更新するのが良い方法です。Mac OS X v10.5では、代わりに<ilcode>kAudioConverterCurrentInputStreamDescription</ilcode>プロパティを使います。+  .. ''mDataFormat''フィールドは、“[[Set Up an Audio Format for Recording]]”で解説されるように、最初はあなたのプログラムのコードによって埋められます。“[[Getting the Full Audio Format from an Audio Queue]]”で解説するように、Audio Queueに''kAudioQueueProperty_StreamDescription''プロパティを問い合わせて、このフィールドを更新するのが良い方法です。Mac OS X v10.5では、代わりに''kAudioConverterCurrentInputStreamDescription''プロパティを使います。 
 +  .. ''AudioStreamBasicDescription''構造体の詳細は、[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/Reference/reference.html|Core Audio Data Types Reference]]をご覧ください。 
 +  - あなたのアプリケーションによって生成される録音Audio Queue。 
 +  - そのAudio Queueによって管理されるAudio Queue Bufferへのポインタの配列。 
 +  - プログラムが音声データを記録するファイルを表すAudio Fileオブジェクト。 
 +  - 各Audio Queue Bufferの大きさのバイト数。この値は、以下のサンプルの''DeriveBufferSize''関数で、Audio Queueが生成されAudio Queueが開始される前に計算されます。“[[Write a Function to Derive Recording Audio Queue Buffer Size]]”をご覧ください。 
 +  - 現在のAudio Queue Bufferから書き出される最初のパケットのパケットインデックス。 
 +  - Audio Queueが実行中かどうかを示すブール値
  
 +===== 録音Audio Queueコールバックの実装 =====
  
-    For details on the AudioStreamBasicDescription structure, see Core Audio Data Types Reference.+次に、録音Audio Queueコールバックを書きます。 
 +このコールバックには2つの目的(main things)があります:
  
-    The recording audio queue created by your application.+  * 新しく埋められたAudio Queue Bufferの内容を記録中のファイルに書き出す。 
 +  * (先ほど中身をディスクに書き出した)Audio Queue Bufferをバッファーキューに加える。
  
-    An array holding pointers to the audio queue buffers managed by the audio queue.+本項ではコールバック宣言の例を示してから、それら2つの仕事を個別に解説し、最後に録音コールバックの全体を示します。 
 +録音Audio Queueコールバックの役割像については、<imgref fig1-3>を見直して下さい。
  
-    An audio file object representing the file into which your program records audio data.+==== 録音Audio Queueコールバックの宣言 ====
  
-    The size, in bytes, for each audio queue buffer. This value is calculated in these examples in the DeriveBufferSize function, after the audio queue is created and before it is started. See “Write a Function to Derive Recording Audio Queue Buffer Size.” +リスト2-2は録音Audio Queueコールバック関数の宣言例で、''AudioQueue.h''ヘッダで''AudioQueueInputCallback''として宣言されています:
- +
-    The packet index for the first packet to be written from the current audio queue buffer. +
- +
-    A Boolean value indicating whether or not the audio queue is running. +
- +
-Write a Recording Audio Queue Callback +
- +
-Next, write a recording audio queue callback function. This callback does two main things: +
- +
-    Writes the contents of a newly filled audio queue buffer to the audio file you’re recording into +
- +
-    Enqueues the audio queue buffer (whose contents were just written to disk) to the buffer queue +
- +
-This section shows an example callback declaration, then describes these two tasks separately, and finally presents an entire recording callback. For an illustration of the role of a recording audio queue callback, you can refer back to Figure 1-3. +
-The Recording Audio Queue Callback Declaration +
- +
-Listing 2-2 shows an example declaration for a recording audio queue callback function, declared as AudioQueueInputCallback in the AudioQueue.h header file: +
- +
-Listing 2-2  The recording audio queue callback declaration+
  
 +**リスト 2-2** 録音Audio Queueコールバック宣言
 +<code c>
 static void HandleInputBuffer ( static void HandleInputBuffer (
- 
     void                                *aqData,             // 1     void                                *aqData,             // 1
- 
     AudioQueueRef                       inAQ,                // 2     AudioQueueRef                       inAQ,                // 2
- 
     AudioQueueBufferRef                 inBuffer,            // 3     AudioQueueBufferRef                 inBuffer,            // 3
- 
     const AudioTimeStamp                *inStartTime,        // 4     const AudioTimeStamp                *inStartTime,        // 4
- 
     UInt32                              inNumPackets,        // 5     UInt32                              inNumPackets,        // 5
- 
     const AudioStreamPacketDescription  *inPacketDesc        // 6     const AudioStreamPacketDescription  *inPacketDesc        // 6
- 
 ) )
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    Typically, aqData is a custom structure that contains state data for the audio queue, as described in Define a Custom Structure to Manage State.+  - 典型的には''aqData''は、[[#状態を管理する独自構造体の定義]]で解説したAudio Queueの状態データを含む独自構造体で 
 +  - このコールバックを所持するAudio Queueです。 
 +  - 録音で入ってくる音声データを含むAudio Queue Bufferです。 
 +  - Audio Queue Bufferの先頭サンプルのサンプル時間です(簡易的な録音では必要ありません)。 
 +  - ''inPacketDesc''パラメータが持つパケット記述子の数。''0''はCBRデータを表します。 
 +  - パケット記述子を必要とする圧縮音声データ形式のための、バッファ内のパケット用のエンコーダが提示するパケット記述子。
  
-    The audio queue that owns this callback.+==== Audio Queue Bufferをディスクへ書き出す ====
  
-    The audio queue buffer containing the incoming audio data to record. +録音Audio Queueコールバックの最初の仕事は、Audio Queue Bufferをディスクに書き出す事です。 
- +このバッファは、たった今コールバックのAudio Queueが入力デバイスからの新しい音声データで埋めたものです。 
-    The sample time of the first sample in the audio queue buffer (not needed for simple recording). +リスト2-3で示すように、コールバックは''AudioFile.h''ヘッダファイルの''AudioFileWritePackets''関数を使います。
- +
-    The number of packet descriptions in the inPacketDesc parameter. A value of 0 indicates CBR data. +
- +
-    For compressed audio data formats that require packet descriptions, the packet descriptions produced by the encoder for the packets in the buffer. +
- +
-Writing an Audio Queue Buffer to Disk +
- +
-The first task of a recording audio queue callback is to write an audio queue buffer to disk. This buffer is the one the callback’s audio queue has just finished filling with new audio data from an input device. The callback uses the AudioFileWritePackets function from the AudioFile.h header file, as shown in Listing 2-3. +
- +
-Listing 2-3  Writing an audio queue buffer to disk+
  
 +**リスト 2-3** Audio Queue Bufferをディスクに書き出す
 +<code c>
 AudioFileWritePackets (                     // 1 AudioFileWritePackets (                     // 1
- 
     pAqData->mAudioFile,                    // 2     pAqData->mAudioFile,                    // 2
- 
     false,                                  // 3     false,                                  // 3
- 
     inBuffer->mAudioDataByteSize,           // 4     inBuffer->mAudioDataByteSize,           // 4
- 
     inPacketDesc,                           // 5     inPacketDesc,                           // 5
- 
     pAqData->mCurrentPacket,                // 6     pAqData->mCurrentPacket,                // 6
- 
     &inNumPackets,                          // 7     &inNumPackets,                          // 7
- 
     inBuffer->mAudioData                    // 8     inBuffer->mAudioData                    // 8
- 
 ); );
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    The AudioFileWritePackets function, declared in the AudioFile.h header file, writes the contents of a buffer to an audio data file.+  - ''AudioFile.h''ヘッダファイルで宣言されている''AudioFileWritePackets''関数は1つのバッファの内容を音声データファイルに書き出します。 
 +  - 音声ファイルオブジェクト(''AudioFileID''型)は書き出し先の音声ファイルを表します。''pAqData''変数はリスト2-1で解説したデータ構造体へのポインタです。 
 +  - ''false''は、関数が書き込み時にデータをキャッシュすべきではない事を示します。 
 +  - 書き出す音声データのバイト数です。''inBuffer''変数はAudio Queueがコールバックに渡したAudio Queue Bufferを表します。 
 +  - 音声データのパケット記述子配列です。NULLはパケット記述子が必要ない事を示します(CBRの音声データなど)。 
 +  - 書き出す先頭パケットのパケットインデックスです。 
 +  - 入力では出力するパケット数を表します。出力では実際に出力されたパケット数が返ります。 
 +  - 音声ファイルに書き出す新しい音声データです。
  
-    The audio file object (of type AudioFileID) that represents the audio file to write to. The pAqData variable is a pointer to the data structure described in Listing 2-1.+==== Audio Buffer Queueをキューに加える ====
  
-    Uses a value of false to indicate that the function should not cache the data when writing. +Audio Queue Bufferの音声データは音声ファイルに書き出されたので、リスト2-4で示す通り、コールバックはそのバッファをキューに戻します。 
- +バッファキューに戻した時点で、そのバッファは待ち行列に並び、次なる入力音声データの受け入れ態勢が整います。
-    The number of bytes of audio data being written. The inBuffer variable represents the audio queue buffer handed to the callback by the audio queue. +
- +
-    An array of packet descriptions for the audio data. A value of NULL indicates no packet descriptions are required (such as for CBR audio data). +
- +
-    The packet index for the first packet to be written. +
- +
-    On input, the number of packets to write. On output, the number of packets actually written. +
- +
-    The new audio data to write to the audio file. +
- +
-Enqueuing an Audio Queue Buffer +
- +
-Now that the audio data from an audio queue buffer has been written to the audio file, the callback enqueues the buffer, as shown in Listing 2-4. Once back in the buffer queue, the buffer is in line and ready to accept more incoming audio data. +
- +
-Listing 2-4  Enqueuing an audio queue buffer after writing to disk+
  
 +**リスト 2-4** ディスクへ出力後のAudio Queue Bufferのエンキュー
 +<code c>
 AudioQueueEnqueueBuffer (                    // 1 AudioQueueEnqueueBuffer (                    // 1
- 
     pAqData->mQueue,                         // 2     pAqData->mQueue,                         // 2
- 
     inBuffer,                                // 3     inBuffer,                                // 3
- 
     0,                                       // 4     0,                                       // 4
- 
     NULL                                     // 5     NULL                                     // 5
- 
 ); );
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    The AudioQueueEnqueueBuffer function adds an audio queue buffer to an audio queue’s buffer queue.+  - ''AudioQueueEnqueueBuffer''関数はAudio Queue BufferをAudio Queueのバッファキューに追加します。 
 +  - 指定したAudio Queue Bufferを追加するAudio Queue。''pAqData''変数はリスト2-1で解説したデータ構造体へのポインタです。 
 +  - キューに追加するAudio Queue Buffer。 
 +  - Audio Queue Bufferのデータのパケット記述子の数。本パラメータは録音では使わないので、''0''を設定します。 
 +  - Audio Queue Bufferのデータのパケット記述子配列。本パラメータは録音では使わないので、''0''を設定します。
  
-    The audio queue to add the designated audio queue buffer to. The pAqData variable is a pointer to the data structure described in Listing 2-1. 
  
-    The audio queue buffer to enqueue.+==== 完全な録音Audio Queueコールバック ====
  
-    The number of packet descriptions in the audio queue buffer's data. Set to 0 because this parameter is unused for recording. +リスト2-5は完全な録音Audio Queueコールバックの基礎バージョンです。 
- +本ドキュメント内の他のコード例と同様、以下のリストはエラー処理を考慮していません。
-    The array of packet descriptions describing the audio queue buffer’s data. Set to NULL because this parameter is unused for recording. +
- +
-A Full Recording Audio Queue Callback +
- +
-Listing 2-5 shows a basic version of a full recording audio queue callback. As with the rest of the code examples in this document, this listing excludes error handling. +
- +
-Listing 2-5  A recording audio queue callback function +
- +
- +
  
 +**リスト 2-5** 録音Audio Queueコールバック関数
 +<code c>
 static void HandleInputBuffer ( static void HandleInputBuffer (
- 
     void                                 *aqData,     void                                 *aqData,
- 
     AudioQueueRef                        inAQ,     AudioQueueRef                        inAQ,
- 
     AudioQueueBufferRef                  inBuffer,     AudioQueueBufferRef                  inBuffer,
- 
     const AudioTimeStamp                 *inStartTime,     const AudioTimeStamp                 *inStartTime,
- 
     UInt32                               inNumPackets,     UInt32                               inNumPackets,
- 
     const AudioStreamPacketDescription   *inPacketDesc     const AudioStreamPacketDescription   *inPacketDesc
- 
 ) { ) {
- 
     AQRecorderState *pAqData = (AQRecorderState *) aqData;               // 1     AQRecorderState *pAqData = (AQRecorderState *) aqData;               // 1
- 
-  
  
     if (inNumPackets == 0 &&                                             // 2     if (inNumPackets == 0 &&                                             // 2
- +        pAqData->mDataFormat.mBytesPerPacket != 0) 
-          pAqData->mDataFormat.mBytesPerPacket != 0) +        inNumPackets = inBuffer->mAudioDataByteSize / pAqData->mDataFormat.mBytesPerPacket;
- +
-       inNumPackets = +
- +
-           inBuffer->mAudioDataByteSize / pAqData->mDataFormat.mBytesPerPacket; +
- +
- +
  
     if (AudioFileWritePackets (                                          // 3     if (AudioFileWritePackets (                                          // 3
- 
             pAqData->mAudioFile,             pAqData->mAudioFile,
- 
             false,             false,
- 
             inBuffer->mAudioDataByteSize,             inBuffer->mAudioDataByteSize,
- 
             inPacketDesc,             inPacketDesc,
- 
             pAqData->mCurrentPacket,             pAqData->mCurrentPacket,
- 
             &inNumPackets,             &inNumPackets,
- 
             inBuffer->mAudioData             inBuffer->mAudioData
- 
         ) == noErr) {         ) == noErr) {
- 
             pAqData->mCurrentPacket += inNumPackets;                     // 4             pAqData->mCurrentPacket += inNumPackets;                     // 4
- 
     }     }
- +    if (pAqData->mIsRunning == 0)                                        // 5 
-   if (pAqData->mIsRunning == 0)                                         // 5 +        return;
- +
-      return; +
- +
- +
  
     AudioQueueEnqueueBuffer (                                            // 6     AudioQueueEnqueueBuffer (                                            // 6
- 
         pAqData->mQueue,         pAqData->mQueue,
- 
         inBuffer,         inBuffer,
- 
         0,         0,
- 
         NULL         NULL
- 
     );     );
- 
 } }
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    The custom structure supplied to the audio queue object upon instantiation, including an audio file object representing the audio file to record into as well as a variety of state data. See Define a Custom Structure to Manage State.+  - インスタンス化を行う際にAudio Queueオブジェクトに供給される、様々な状態データと共に書き出し先の音声ファイルを表すAudio Fileオブジェクトを含む独自構造体。[[状態を管理する独自構造体の定義]]をご覧ください。 
 +  - Audio Queue BufferがCBRデータを含む場合、バッファ内のパケット数を算出する。この値はバッファ内の合計バイト数を1パケット毎のバイト数(定数)で割った値に等しい。VBRデータでは、Audio Queueはコールバック呼び出し時のバッファ内のパケットデータ数を提供する。 
 +  - バッファの内容を音声データファイルに書き出す。詳しい解説は“[[#Audio Queue Bufferをディスクへ書き出す]]”をご覧ください。 
 +  - 音声データの書き出しが成功すれば、音声データファイルのパケットインデックスをインクリメントし、バッファの次の音声データ書き出しに備える。 
 +  - Audio Queueが停止していたら関数を抜ける。 
 +  - 今しがた中身が音声ファイルに書き出されたAudio Queue Bufferをキューに戻す。詳しい解説は“[[Audio Buffer Queueをキューに加える]]”をご覧ください。
  
-    If the audio queue buffer contains CBR data, calculate the number of packets in the buffer. This number equals the total bytes of data in the buffer divided by the (constant) number of bytes per packet. For VBR data, the audio queue supplies the number of packets in the buffer when it invokes the callback.+===== 録音Audio Queue Bufferサイズを計算する関数の実装 =====
  
-    Writes the contents of the buffer to the audio data file. For a detailed description , see “Writing an Audio Queue Buffer to Disk.+Audio Queue Serviceは使用するAudio Queue Bufferサイズの特定を、アプリケーションが行うと期待します。 
 +リスト2-6はその方法の1つです。 
 +It derives a buffer size large enough to hold a given duration of audio data.
  
-    If successful in writing the audio data, increment the audio data file’s packet index to be ready for writing the next buffer's worth of audio data. +ここでの計算は録音する音声データ形式を考慮に入れます。 
- +フォーマットは、音声チャンネル数などの、バッファサイズに影響するであろう全ての要因を含みます。
-    If the audio queue has stopped, return. +
- +
-    Enqueues the audio queue buffer whose contents have just been written to the audio file. For a detailed description, see “Enqueuing an Audio Queue Buffer.” +
- +
-Write a Function to Derive Recording Audio Queue Buffer Size +
- +
-Audio Queue Services expects your application to specify a size for the audio queue buffers you use. Listing 2-6 shows one way to do this. It derives a buffer size large enough to hold a given duration of audio data. +
- +
-The calculation here takes into account the audio data format you’re recording to. The format includes all the factors that might affect buffer size, such as the number of audio channels. +
- +
-Listing 2-6  Deriving a recording audio queue buffer size+
  
 +**リスト 2-6** 録音Audio Queue Bufferサイズの算出
 +<code c>
 void DeriveBufferSize ( void DeriveBufferSize (
- 
     AudioQueueRef                audioQueue,                  // 1     AudioQueueRef                audioQueue,                  // 1
- 
     AudioStreamBasicDescription  &ASBDescription,             // 2     AudioStreamBasicDescription  &ASBDescription,             // 2
- 
     Float64                      seconds,                     // 3     Float64                      seconds,                     // 3
- 
     UInt32                       *outBufferSize               // 4     UInt32                       *outBufferSize               // 4
- 
 ) { ) {
- 
     static const int maxBufferSize = 0x50000;                 // 5     static const int maxBufferSize = 0x50000;                 // 5
- 
-  
  
     int maxPacketSize = ASBDescription.mBytesPerPacket;       // 6     int maxPacketSize = ASBDescription.mBytesPerPacket;       // 6
- 
     if (maxPacketSize == 0) {                                 // 7     if (maxPacketSize == 0) {                                 // 7
- 
         UInt32 maxVBRPacketSize = sizeof(maxPacketSize);         UInt32 maxVBRPacketSize = sizeof(maxPacketSize);
- 
         AudioQueueGetProperty (         AudioQueueGetProperty (
- 
                 audioQueue,                 audioQueue,
- 
                 kAudioQueueProperty_MaximumOutputPacketSize,                 kAudioQueueProperty_MaximumOutputPacketSize,
- 
                 // in Mac OS X v10.5, instead use                 // in Mac OS X v10.5, instead use
- 
                 //   kAudioConverterPropertyMaximumOutputPacketSize                 //   kAudioConverterPropertyMaximumOutputPacketSize
- 
                 &maxPacketSize,                 &maxPacketSize,
- 
                 &maxVBRPacketSize                 &maxVBRPacketSize
- 
         );         );
- 
     }     }
- 
-  
  
     Float64 numBytesForTime =     Float64 numBytesForTime =
- 
         ASBDescription.mSampleRate * maxPacketSize * seconds; // 8         ASBDescription.mSampleRate * maxPacketSize * seconds; // 8
- 
     *outBufferSize =     *outBufferSize =
- +        UInt32 (numBytesForTime < maxBufferSize ? 
-    UInt32 (numBytesForTime < maxBufferSize ? +                numBytesForTime : maxBufferSize);             // 9
- +
-        numBytesForTime : maxBufferSize);                     // 9 +
 } }
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    The audio queue that owns the buffers whose size you want to specify.+  - サイズを特定したいバッファを持つAudio Queue。 
 +  - Audio Queueの''AudioStreamBasicDescription''構造体。 
 +  - 音声の秒数を単位とした、必要とするAudio Queue Bufferの大きさ。 
 +  - 出力で、バイト数を単位としたAudio Aueue Bufferのサイズ。 
 +  - Audio Queue Bufferサイズの上限(バイト数)。この例では320KBに設定されている。これはサンプリング周波数96kHz/24ビット/ステレオでおよそ5秒に対応する。 
 +  - CBR音声データ用に、''AudioStreamBasicDescription''構造体からパケットサイズ(定数)を得る。この値を最大パケットサイズとして使う。 
 +  - この割り当ては、その音声データの録音がCBRかVBRかを特定する副作用をもたらす。VBRならば、Audio Queueの''AudioStreamBasicDescription''構造体のbytes-per-packetの値は0を示す。 
 +  - VBR音声データ用に、Audio Queueに見積もり最大パケットサイズを問い合わせる。 
 +  - バイト数でバッファサイズを計算する。 
 +  - 必要ならば、前述の上限値にバッファサイズを制限する。
  
-    The AudioStreamBasicDescription structure for the audio queue.+===== Audio File用マジッククッキーの設定 =====
  
-    The size you are specifying for each audio queue buffer, in terms of seconds of audio.+MPEG-4 AACなどのいくつかの圧縮音声形式は、オーディオメタデータを含む構造を利用します。 
 +これら構造を**マジッククッキー**と呼びます。 
 +Audio Queue Serviceを使ってこのような形式で録音する際は、録音開始前にAudio Queueからマジッククッキーを取得し、それをAudio Fileに追加しなければなりません。
  
-    On output, the size for each audio queue buffer, in terms of bytes. +リスト2-7はAudio Queueからマジッククッキーを得る方法と、Audio Fileへの適用方法を示します。 
- +Your code would call a function like this before recording, and then again after recording—some codecs update magic cookie data when recording has stopped.
-    An upper bound for the audio queue buffer size, in bytes. In this example, the upper bound is set to 320 KB. This corresponds to approximately five seconds of stereo, 24 bit audio at a sample rate of 96 kHz. +
- +
-    For CBR audio data, get the (constant) packet size from the AudioStreamBasicDescription structure. Use this value as the maximum packet size. +
- +
-    This assignment has the side effect of determining if the audio data to be recorded is CBR or VBR. If it is VBR, the audio queue’s AudioStreamBasicDescription structure lists the value of bytes-per-packet as 0. +
- +
-    For VBR audio data, query the audio queue to get the estimated maximum packet size. +
- +
-    Derive the buffer size, in bytes. +
- +
-    Limit the buffer size, if needed, to the previously set upper bound. +
- +
-Set a Magic Cookie for an Audio File +
- +
-Some compressed audio formats, such as MPEG 4 AAC, make use of structures that contain audio metadata. These structures are called magic cookies. When you record to such a format using Audio Queue Services, you must get the magic cookie from the audio queue and add it to the audio file before you start recording. +
- +
-Listing 2-7 shows how to obtain a magic cookie from an audio queue and apply it to an audio file. Your code would call a function like this before recording, and then again after recording—some codecs update magic cookie data when recording has stopped. +
- +
-Listing 2-7  Setting a magic cookie for an audio file+
  
 +**リスト 2-7** Audio File用マジッククッキーの設定
 +<code c_mac>
 OSStatus SetMagicCookieForFile ( OSStatus SetMagicCookieForFile (
- 
     AudioQueueRef inQueue,                                      // 1     AudioQueueRef inQueue,                                      // 1
- 
     AudioFileID   inFile                                        // 2     AudioFileID   inFile                                        // 2
- 
 ) { ) {
- 
     OSStatus result = noErr;                                    // 3     OSStatus result = noErr;                                    // 3
- 
     UInt32 cookieSize;                                          // 4     UInt32 cookieSize;                                          // 4
- 
-  
  
     if (     if (
- 
             AudioQueueGetPropertySize (                         // 5             AudioQueueGetPropertySize (                         // 5
- 
                 inQueue,                 inQueue,
- 
                 kAudioQueueProperty_MagicCookie,                 kAudioQueueProperty_MagicCookie,
- 
                 &cookieSize                 &cookieSize
- 
             ) == noErr             ) == noErr
- 
     ) {     ) {
- 
         char* magicCookie =         char* magicCookie =
- 
             (char *) malloc (cookieSize);                       // 6             (char *) malloc (cookieSize);                       // 6
- 
         if (         if (
- 
                 AudioQueueGetProperty (                         // 7                 AudioQueueGetProperty (                         // 7
- 
                     inQueue,                     inQueue,
- 
                     kAudioQueueProperty_MagicCookie,                     kAudioQueueProperty_MagicCookie,
- 
                     magicCookie,                     magicCookie,
- 
                     &cookieSize                     &cookieSize
- 
                 ) == noErr                 ) == noErr
- 
         )         )
- 
             result =    AudioFileSetProperty (                  // 8             result =    AudioFileSetProperty (                  // 8
- 
                             inFile,                             inFile,
- 
                             kAudioFilePropertyMagicCookieData,                             kAudioFilePropertyMagicCookieData,
- 
                             cookieSize,                             cookieSize,
- 
                             magicCookie                             magicCookie
- 
                         );                         );
- 
         free (magicCookie);                                     // 9         free (magicCookie);                                     // 9
- 
     }     }
- 
     return result;                                              // 10     return result;                                              // 10
- 
 } }
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    The audio queue you’re using for recording.+  - 録音に使うAudio Queue。 
 +  - 記録に使うAudio File。 
 +  - この関数の成否を表す返値変数。 
 +  - マジッククッキーのデータサイズを保持する変数。 
 +  - Audio Queueからマジッククッキーのデータサイズを取得し、''cookieSize''変数に格納します。 
 +  - マジッククッキー情報を保持するバイト列を確保します。 
 +  - Audio Queueの''kAudioQueueProperty_MagicCookie''プロパティを問い合わせてマジッククッキーを取得します。 
 +  - 記録するAudio Fileにマジッククッキーを設定します。''AudioFileSetProperty''関数は''AudioFile.h''ヘッダファイルで宣言されています。 
 +  - 一時クッキー変数のメモリを解放します。 
 +  - 関数の成否を返します。
  
-    The audio file you’re recording into.+===== 録音音声フォーマットの構成 =====
  
-    A result variable that indicates the success or failure of this function.+本項ではAudio Queueの音声データ形式の構成方法を説明します。 
 +Audio Queueはファイルへの記録にこのフォーマットを使用します。
  
-    A variable to hold the magic cookie data size.+音声データ形式の構成には、次の事柄を特定します:
  
-    Gets the data size of the magic cookie from the audio queue and stores it in the cookieSize variable.+  * 音声データ形式の種類(リニアPCM, AAC, その他など) 
 +  * サンプリング周波数(44.1kHzなど) 
 +  * 音声チャンネル数(ステレオには2など) 
 +  * ビット深度(16ビットなど) 
 +  * パケット毎のフレーム数(例えばリニアPCMでは1フレーム毎パケット) 
 +  * 音声ファイルの種類(CAF, AIFF, その他など) 
 +  * そのファイル種別が要求する音声データ形式の詳細
  
-    Allocates an array of bytes to hold the magic cookie information. +リスト2-8は、各属性を固定値とした録音用の音声形式の構成を示します。 
- +製品コードでは、通常、音声形式の幾つかないし全ての項目をユーザーが設定できるようにすべきでしょう。 
-    Gets the magic cookie by querying the audio queue’s kAudioQueueProperty_MagicCookie property. +いずれのアプローチでも、目的は[[#状態を管理する独自構造体の定義]]で解説した''AQRecorderState''独自構造体の''mDataFormat''のフィールドを埋めることです。
- +
-    Sets the magic cookie for the audio file you’re recording into. The AudioFileSetProperty function is declared in the AudioFile.h header file. +
- +
-    Frees the memory for the temporary cookie variable. +
- +
-    Returns the success or failure of this function. +
- +
-Set Up an Audio Format for Recording +
- +
-This section describes how you set up an audio data format for the audio queue. The audio queue uses this format for recording to a file. +
- +
-To set up an audio data format, you specify: +
- +
-    Audio data format type (such as linear PCM, AAC, etc.) +
- +
-    Sample rate (such as 44.1 kHz) +
- +
-    Number of audio channels (such as 2, for stereo) +
- +
-    Bit depth (such as 16 bits) +
- +
-    Frames per packet (linear PCM, for example, uses one frame per packet) +
- +
-    Audio file type (such as CAF, AIFF, etc.) +
- +
-    Details of the audio data format required for the file type +
- +
-Listing 2-8 illustrates setting up an audio format for recording, using a fixed choice for each attribute. In production code, you’d typically allow the user to specify some or all aspects of the audio format. With either approach, the goal is to fill the mDataFormat field of the AQRecorderState custom structure, described in Define a Custom Structure to Manage State. +
- +
-Listing 2-8  Specifying an audio queue’s audio data format+
  
 +**リスト 2-8** Audio Queueの音声データ形式の指定
 +<code c_mac>
 AQRecorderState aqData;                                       // 1 AQRecorderState aqData;                                       // 1
- 
-  
  
 aqData.mDataFormat.mFormatID         = kAudioFormatLinearPCM; // 2 aqData.mDataFormat.mFormatID         = kAudioFormatLinearPCM; // 2
- 
 aqData.mDataFormat.mSampleRate       = 44100.0;               // 3 aqData.mDataFormat.mSampleRate       = 44100.0;               // 3
- 
 aqData.mDataFormat.mChannelsPerFrame = 2;                     // 4 aqData.mDataFormat.mChannelsPerFrame = 2;                     // 4
- 
 aqData.mDataFormat.mBitsPerChannel   = 16;                    // 5 aqData.mDataFormat.mBitsPerChannel   = 16;                    // 5
- 
 aqData.mDataFormat.mBytesPerPacket                          // 6 aqData.mDataFormat.mBytesPerPacket                          // 6
- +aqData.mDataFormat.mBytesPerFrame    
-   aqData.mDataFormat.mBytesPerFrame = +    aqData.mDataFormat.mChannelsPerFrame * sizeof (SInt16);
- +
-      aqData.mDataFormat.mChannelsPerFrame * sizeof (SInt16); +
 aqData.mDataFormat.mFramesPerPacket  = 1;                     // 7 aqData.mDataFormat.mFramesPerPacket  = 1;                     // 7
- 
-  
  
 AudioFileTypeID fileType             = kAudioFileAIFFType;    // 8 AudioFileTypeID fileType             = kAudioFileAIFFType;    // 8
- +aqData.mDataFormat.mFormatFlags                             // 9
-aqData.mDataFormat.mFormatFlags =                             // 9 +
     kLinearPCMFormatFlagIsBigEndian     kLinearPCMFormatFlagIsBigEndian
- 
     | kLinearPCMFormatFlagIsSignedInteger     | kLinearPCMFormatFlagIsSignedInteger
- 
     | kLinearPCMFormatFlagIsPacked;     | kLinearPCMFormatFlagIsPacked;
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    Creates an instance of the AQRecorderState custom structure. The structure’s mDataFormat field contains an AudioStreamBasicDescription structure. The values set in the mDataFormat field provide an initial definition of the audio format for the audio queuewhich is also the audio format for the file you record into. In Listing 2-10, you obtain a more complete specification of the audio format, which Core Audio provides to you based on the format type and file type.+  - ''AQRecorderState''独自構造体の実体を生成します。構造体の''mDataFormat''フィールドは''AudioStreamBasicDescription''構造体を含みます。''mDataFormat''フィールドでセットされた値は、Audio Queueの音声フォーマットの初期定義を提供しますそれはまた、記録先ファイルの音声フォーマットでもあります。リスト2-10では、より完全な音声フォーマット仕様を取得しますが、Core Audioはそれらをフォーマットタイプとファイルタイプに基づいて提供します。 
 +  - 音声データタイプをリニアPCMとして定義します。利用可能なデータ形式の完全なリストは//[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/Reference/reference.html|Core Audio Data Types Reference]]//をご覧下さい。 
 +  - サンプリング周波数を44.1kHzとします。 
 +  - チャンネル数は2とします。 
 +  - チャンネルあたりのビット深度は16とします。 
 +  - パケットあたりのバイト数とフレームあたりのバイト数は4とします(2チャンネル×サンプルあたり2バイト)。 
 +  - パケットあたりのフレーム数は1とします。 
 +  - ファイルタイプはAIFFとします。利用可能なファイルタイプの完全なリストは''AudioFile.h''ヘッダファイルの音声ファイルタイプ列挙値をご覧下さい。“[[0200_aboutaudioqueues#コーデックと音声データ形式を使う]]”で解説したように、インストール済みコーデックが存在するあらゆるファイルタイプを書くことが出来ます。 
 +  - 特定のファイルタイプで必要となるフォーマットフラグを設定します。
  
-    Defines the audio data format type as linear PCM. See Core Audio Data Types Reference for a complete listing of the available data formats.+===== 録音Audio Queueの生成 =====
  
-    Defines the sample rate as 44.1 kHz.+それでは、録音コールバックと音声データ形式を設定して、録音Audio Queueの生成と設定をします。
  
-    Defines the number of channels as 2.+==== 録音Audio Queueを生成する ====
  
-    Defines the bit depth per channel as 16. +リスト2-9は録音Audio Queueの作り方を示したものです。 
- +''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html#//apple_ref/doc/c_ref/AudioQueueNewInput|AudioQueueNewInput]]''関数が、前のステップで構成したコールバック、独自構造体、音声データ形式を使う点に注目して下さい。
-    Defines the number of bytes per packet, and the number of bytes per frame, to 4 (that is, channels times 2 bytes per sample). +
- +
-    Defines the number of frames per packet as 1. +
- +
-    Defines the file type as AIFF. See the audio file types enumeration in the AudioFile.h header file for a complete listing of the available file types. You can specify any file type for which there is an installed codec, as described in “Using Codecs and Audio Data Formats.” +
- +
-    Sets the format flags needed for the specified file type. +
- +
-Create a Recording Audio Queue +
- +
-Now, with the recording callback and audio data format set up, you create and configure an audio queue for recording. +
-Creating a Recording Audio Queue +
- +
-Listing 2-9 illustrates how to create a recording audio queueNotice that the AudioQueueNewInput function uses the callback, the custom structure, and the audio data format that were configured in previous steps. +
- +
-Listing 2-9  Creating a recording audio queue+
  
 +**リスト 2-9** 録音Audio Queueの生成
 +<code c_mac>
 AudioQueueNewInput (                              // 1 AudioQueueNewInput (                              // 1
- 
     &aqData.mDataFormat,                          // 2     &aqData.mDataFormat,                          // 2
- 
     HandleInputBuffer,                            // 3     HandleInputBuffer,                            // 3
- 
     &aqData,                                      // 4     &aqData,                                      // 4
- 
     NULL,                                         // 5     NULL,                                         // 5
- 
     kCFRunLoopCommonModes,                        // 6     kCFRunLoopCommonModes,                        // 6
- 
     0,                                            // 7     0,                                            // 7
- 
     &aqData.mQueue                                // 8     &aqData.mQueue                                // 8
- 
 ); );
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    The AudioQueueNewInput function creates a new recording audio queue.+  - ''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html#//apple_ref/doc/c_ref/AudioQueueNewInput|AudioQueueNewInput]]''関数は新しい録音Audio Queueを生成します。 
 +  - 録音に使う音声データ形式です。“[[#録音用音声フォーマットの構成]]”をご覧下さい。 
 +  - 録音Audio Queueが使うコールバック関数です。“[[#録音Audio Queueコールバックの実装]]”をご覧下さい。 
 +  - 録音Audio Queueの独自データ構造体です。“[[#状態を管理する独自構造体の定義]]”をご覧下さい。 
 +  - コールバックを呼び出す実行ループを指定します。''NULL''はデフォルトの挙動を示し、Audio Queue内部のスレッドでコールバックが呼ばれます。これは典型的な使い方で、アプリケーションのユーザーインタフェーススレッドが録音停止の入力を待つ間、Audio Queueによる録音を許可します。 
 +  - コールバック呼び出しの実行ループモードです。通常、ここには''kCFRunLoopCommonModes''定数を使います。 
 +  - 将来拡張用です。''0''を指定して下さい。 
 +  - 新たに確保された録音Audio Queueが出力されます。
  
-    The audio data format to use for the recording. See “Set Up an Audio Format for Recording.”+==== Audio Queueから完全な音声形式を取得する ====
  
-    The callback function to use with the recording audio queue. See Write a Recording Audio Queue Callback.+Audio Queueの実体が出来ると([[#録音Audio Queueの生成]]”をご覧下さい)、Audio Queueは特に圧縮フォーマットにおいて、あなたが持っているものより完全に''AudioStreamBasicDescription''構造体を埋めるでしょう。 
 +完全なフォーマット記述を取得するには、リスト2-10で示すように''AudioQueueGetProperty''関数を呼びます。 
 +記録先のAudio Fileを作る際は、その完全な音声フォーマットをしようします(“[[#Audio Fileの生成]]をご覧下さい)。
  
-    The custom data structure for the recording audio queue. See “Define a Custom Structure to Manage State.” +**リスト 2-10** Audio Queueから音声フォーマットを得る
- +
-    The run loop on which the callback will be invoked. Use NULL to specify default behavior, in which the callback will be invoked on a thread internal to the audio queue. This is typical use—it allows the audio queue to record while your application’s user interface thread waits for user input to stop the recording. +
- +
-    The run loop modes in which the callback can be invoked. Normally, use the kCFRunLoopCommonModes constant here. +
- +
-    Reserved. Must be 0. +
- +
-    On output, the newly allocated recording audio queue. +
- +
-Getting the Full Audio Format from an Audio Queue +
- +
-When the audio queue came into existence (see “Creating a Recording Audio Queue”), it may have filled out the AudioStreamBasicDescription structure more completely than you have, particularly for compressed formats. To obtain the complete format description, call the AudioQueueGetProperty function as shown in Listing 2-10. You use the complete audio format when you create an audio file to record into (see “Create an Audio File”). +
- +
-Listing 2-10  Getting the audio format from an audio queue+
  
 +<code c_mac>
 UInt32 dataFormatSize = sizeof (aqData.mDataFormat);       // 1 UInt32 dataFormatSize = sizeof (aqData.mDataFormat);       // 1
- 
-  
  
 AudioQueueGetProperty (                                    // 2 AudioQueueGetProperty (                                    // 2
- 
     aqData.mQueue,                                         // 3     aqData.mQueue,                                         // 3
- 
     kAudioQueueProperty_StreamDescription,                 // 4     kAudioQueueProperty_StreamDescription,                 // 4
- +    // Mac OS Xでは代わりに↓を使ってください
-    // in Mac OS X, instead use +
     //    kAudioConverterCurrentInputStreamDescription     //    kAudioConverterCurrentInputStreamDescription
- 
     &aqData.mDataFormat,                                   // 5     &aqData.mDataFormat,                                   // 5
- 
     &dataFormatSize                                        // 6     &dataFormatSize                                        // 6
- 
 ); );
 +</code>
 +コードの働きを見てみましょう:
  
-Here’s how this code works:+  - Audio Queueに音声データ形式を問い合わせる時に使う、プロパティ値の期待サイズを得ます。 
 +  - ''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html#//apple_ref/doc/c_ref/AudioQueueGetProperty|AudioQueueGetProperty]]''関数はAudio Queueから指定のプロパティ値を得ます。 
 +  - 音声データ形式を取得するAudio Queueです。 
 +  - Audio Queueのデータ形式値を得るためのプロパティIDです。 
 +  - 出力で、Audio Queueから得られた''AudioStreamBasicDescription''構造体による完全な音声データ形式です。 
 +  - 入力では、''AudioStreamBasicDescription''構造体の期待サイズです。出力では、実際のサイズが入ります。あなたの録音アプリケーションでは、この値を利用する必要はありません。
  
-    Gets an expected property value size to use when querying the audio queue about its audio data format.+===== Audio Fileの生成 =====
  
-    The AudioQueueGetProperty function obtains the value for a specified property in an audio queue.+生成と設定が行われたAudio Queueを使って、リスト2-11のように録音データを保存するAudio Fileを生成します。 
 +Audio Fileは、前述のAudio Queueの独自構造体に格納されたデータ形式とファイル形式仕様を使います。
  
-    The audio queue to obtain the audio data format from. +**リスト 2-11** 記録用Audio Fileの生成
- +
-    The property ID for obtaining the value of the audio queue’s data format. +
- +
-    On output, the full audio data format, in the form of an AudioStreamBasicDescription structure, obtained from the audio queue. +
- +
-    On input, the expected size of the AudioStreamBasicDescription structure. On output, the actual size. Your recording application does not need to make use of this value. +
- +
-Create an Audio File +
- +
-With an audio queue created and configured, you create the audio file that you’ll record audio data into, as shown in Listing 2-11. The audio file uses the data format and file format specifications previously stored in the audio queue’s custom structure. +
- +
-Listing 2-11  Creating an audio file for recording+
  
 +<code c_mac>
 CFURLRef audioFileURL = CFURLRef audioFileURL =
- 
     CFURLCreateFromFileSystemRepresentation (            // 1     CFURLCreateFromFileSystemRepresentation (            // 1
- 
         NULL,                                            // 2         NULL,                                            // 2
- 
         (const UInt8 *) filePath,                        // 3         (const UInt8 *) filePath,                        // 3
- 
         strlen (filePath),                               // 4         strlen (filePath),                               // 4
- 
         false                                            // 5         false                                            // 5
- 
     );     );
- 
-  
  
 AudioFileCreateWithURL (                                 // 6 AudioFileCreateWithURL (                                 // 6
- 
     audioFileURL,                                        // 7     audioFileURL,                                        // 7
- 
     fileType,                                            // 8     fileType,                                            // 8
- 
     &aqData.mDataFormat,                                 // 9     &aqData.mDataFormat,                                 // 9
- 
     kAudioFileFlags_EraseFile,                           // 10     kAudioFileFlags_EraseFile,                           // 10
- 
     &aqData.mAudioFile                                   // 11     &aqData.mAudioFile                                   // 11
- 
 ); );
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    The CFURLCreateFromFileSystemRepresentation function, declared in the CFURL.h header file, creates a CFURL object representing a file to record into.+  - ''CFURL.h''ヘッダファイルで宣言される''[[https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFURLRef/Reference/reference.html#//apple_ref/doc/c_ref/CFURLCreateFromFileSystemRepresentation|CFURLCreateFromFileSystemRepresentation]]''関数は、記録に使うファイルを表す''CFURL''オブジェクトを生成します。 
 +  - ''NULL''(または''kCFAllocatorDefault'')を使うと、現在のデフォルトメモリアロケータを使います。 
 +  - ''CFURL''オブジェクトに変換したいファイルシステムパスです。製品コードでは、通常、''filePath''からユーザー指定の値を得ます。 
 +  - ファイルシステムパスのバイト数です。 
 +  - ''false''は''filePath''がディレクトリではなくファイルである事を示します。 
 +  - ''AudioFile.h''ヘッダファイルの''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioFileConvertRef/Reference/reference.html#//apple_ref/doc/c_ref/AudioFileCreateWithURL|AudioFileCreateWithURL]]''関数は、音声ファイルの新規作成ないし既存ファイルの初期化を行います。 
 +  - 新規音声ファイルの作成、もしくは既存ファイルの初期化の対象となるURLです。URLは手順1の[[https://developer.apple.com/library/mac/documentation/CoreFoundation/Reference/CFURLRef/Reference/reference.html#//apple_ref/doc/c_ref/CFURLCreateFromFileSystemRepresentation|CFURLCreateFromFileSystemRepresentation]]に由来します。 
 +  - 新しいファイルのファイルタイプです。本章のサンプルコードでは、前に''kAudioFileAIFFType''ファイルタイプ定数を経由してAIFFに設定されています。“[[#録音音声フォーマットの構成]]”をご覧下さい。 
 +  - ''AudioStreamBasicDescription''構造体として指定される、ファイルに記録する音声のデータ形式です。本章のサンプルコードでは、これも“[[#録音音声フォーマットの構成]]”で設定されています。 
 +  - ファイルが既に存在していた場合、そのファイルを消します。 
 +  - 出力で、記録先の音声ファイルを表す(''AudioFileID''型の)Audio Fileオブジェクトです。
  
-    Use NULL (or kCFAllocatorDefault) to use the current default memory allocator.+===== Audio Queue Bufferサイズの設定 =====
  
-    The file-system path you want to convert to a CFURL object. In production code, you would typically obtain a value for filePath from the user.+録音中に使うAudio Queue Bufferセットを準備する前に、先に書いた''DeriveBufferSize''関数(“[[#録音Audio Queue Bufferサイズを計算する関数の実装]]”をご覧下さい)を使います。 
 +このサイズを使用する録音Audio Queueのサイズに割り当てます。 
 +リスト2-12がその様子を示します:
  
-    The number of bytes in the file-system path. +**リスト 2-12** Audio Queue Bufferサイズの設定
- +
-    A value of false indicates that filePath represents a file, not a directory. +
- +
-    The AudioFileCreateWithURL function, from the AudioFile.h header file, creates a new audio file or initializes an existing file. +
- +
-    The URL at which to create the new audio file, or to initialize in the case of an existing file. The URL was derived from the CFURLCreateFromFileSystemRepresentation in step 1. +
- +
-    The file type for the new file. In the example code in this chapter, this was previously set to AIFF by way of the kAudioFileAIFFType file type constant. See “Set Up an Audio Format for Recording.” +
- +
-    The data format of the audio that will be recorded into the file, specified as an AudioStreamBasicDescription structure. In the example code for this chapter, this was also set in “Set Up an Audio Format for Recording.” +
- +
-    Erases the file, in the case that the file already exists. +
- +
-    On output, an audio file object (of type AudioFileID) representing the audio file to record into. +
- +
-Set an Audio Queue Buffer Size +
- +
-Before you prepare a set of audio queue buffers that you’ll use while recording, you make use of the DeriveBufferSize function you wrote earlier (see “Write a Function to Derive Recording Audio Queue Buffer Size”). You assign this size to the recording audio queue you are using. Listing 2-12 illustrates this: +
- +
-Listing 2-12  Setting an audio queue buffer size+
  
 +<code c_mac>
 DeriveBufferSize (                               // 1 DeriveBufferSize (                               // 1
- 
     aqData.mQueue,                               // 2     aqData.mQueue,                               // 2
- 
     aqData.mDataFormat,                          // 3     aqData.mDataFormat,                          // 3
- 
     0.5,                                         // 4     0.5,                                         // 4
- 
     &aqData.bufferByteSize                       // 5     &aqData.bufferByteSize                       // 5
- 
 ); );
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    The DeriveBufferSize function, described in Write a Function to Derive Recording Audio Queue Buffer Size,” sets an appropriate audio queue buffer size.+  - [[#録音Audio Queue Bufferサイズを計算する関数の実装]]で解説した''DeriveBufferSize''関数は、妥当なAudio Queue Bufferサイズを設定します。 
 +  - バッファサイズを設定するAudio Queueです。 
 +  - 記録するファイルの音声データ形式です。“[[#録音音声フォーマットの構成]]”をご覧下さい。 
 +  - 各Audio Queue Bufferが保持する音声の秒数です。通常、ここで設定している0.5秒が良い選択です。 
 +  - 出力で、各Audio Queue Bufferサイズがバイト数で返ります。この値はAudio Queueの独自構造体の中に置かれます。
  
-    The audio queue that you’re setting buffer size for.+===== Audio Queue Bufferセットの準備 =====
  
-    The audio data format for the file you are recording. See Set Up an Audio Format for Recording. +それでは、([[#録音Audio Queueの生成]]で)生成したAudio Queueに、Audio Queue Bufferセットを準備するように命令します。 
- +リスト 2-13はそのデモンストレーションです。
-    The number of seconds of audio that each audio queue buffer should hold. One half second, as set here, is typically a good choice. +
- +
-    On output, the size for each audio queue buffer, in bytes. This value is placed in the custom structure for the audio queue. +
- +
-Prepare a Set of Audio Queue Buffers +
- +
-You now ask the audio queue that you’ve created (in “Create a Recording Audio Queue”) to prepare a set of audio queue buffers. Listing 2-13 demonstrates how to do this. +
- +
-Listing 2-13  Preparing a set of audio queue buffers+
  
 +**リスト 2-13** Audio Queue Bufferセットの準備
 +<code c_mac>
 for (int i = 0; i < kNumberBuffers; ++i) {           // 1 for (int i = 0; i < kNumberBuffers; ++i) {           // 1
- 
     AudioQueueAllocateBuffer (                       // 2     AudioQueueAllocateBuffer (                       // 2
- 
         aqData.mQueue,                               // 3         aqData.mQueue,                               // 3
- 
         aqData.bufferByteSize,                       // 4         aqData.bufferByteSize,                       // 4
- 
         &aqData.mBuffers[i]                          // 5         &aqData.mBuffers[i]                          // 5
- 
     );     );
- 
-  
  
     AudioQueueEnqueueBuffer (                        // 6     AudioQueueEnqueueBuffer (                        // 6
- 
         aqData.mQueue,                               // 7         aqData.mQueue,                               // 7
- 
         aqData.mBuffers[i],                          // 8         aqData.mBuffers[i],                          // 8
- 
         0,                                           // 9         0,                                           // 9
- 
         NULL                                         // 10         NULL                                         // 10
- 
     );     );
- 
 } }
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    Iterates to allocate and enqueue each audio queue buffer.+  - Audio Queue Buffer確保とキュー追加のためにイテレートします。 
 +  - ''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html#//apple_ref/doc/c_ref/AudioQueueAllocateBuffer|AudioQueueAllocateBuffer]]''関数はAudio QueueにAudio Queue Bufferを確保するよう指示します。 
 +  - バッファを確保し所持するAudio Queueです。 
 +  - 新しく確保されるAudio Queue Bufferのバイト数です。“[[#録音Audio Queue Bufferサイズを計算する関数の実装]]”をご覧下さい。 
 +  - 新しく確保されたAudio Queue Bufferが出力されます。バッファへのポインタはAudio Queueと共に使う独自構造体の中に置かれます。 
 +  - ''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html#//apple_ref/doc/c_ref/AudioQueueEnqueueBuffer|AudioQueueEnqueueBuffer]]''関数はバッファキューの最後にAudio Queue Bufferを追加します。 
 +  - キューに加えるAudio Queue Bufferを持つAudio Queue。 
 +  - キューに加えるAudio Queue Buffer。 
 +  - 録音におけるバッファのキュー追加では、本パラメータは使用しません。 
 +  - 録音におけるバッファのキュー追加では、本パラメータは使用しません。
  
-    The AudioQueueAllocateBuffer function asks an audio queue to allocate an audio queue buffer.+===== 録音 =====
  
-    The audio queue that performs the allocation and that will own the buffer. +リスト2-14で示されるように、これまでのコードの全てが録音のとてもシンプルな工程に繋がりました。
- +
-    The size, in bytes, for the new audio queue buffer being allocated. See “Write a Function to Derive Recording Audio Queue Buffer Size.” +
- +
-    On output, the newly allocated audio queue buffer. The pointer to the buffer is placed in the custom structure you’re using with the audio queue. +
- +
-    The AudioQueueEnqueueBuffer function adds an audio queue buffer to the end of a buffer queue. +
- +
-    The audio queue whose buffer queue you are adding the buffer to. +
- +
-    The audio queue buffer you are enqueuing. +
- +
-    This parameter is unused when enqueuing a buffer for recording. +
- +
-    This parameter is unused when enqueuing a buffer for recording. +
- +
-Record Audio +
- +
-All of the preceding code has led up to the very simple process of recording, as shown in Listing 2-14+
- +
-Listing 2-14  Recording audio+
  
 +**リスト 2-14** 録音
 +<code c_mac>
 aqData.mCurrentPacket = 0;                           // 1 aqData.mCurrentPacket = 0;                           // 1
- 
 aqData.mIsRunning = true;                            // 2 aqData.mIsRunning = true;                            // 2
- 
-  
  
 AudioQueueStart (                                    // 3 AudioQueueStart (                                    // 3
- 
     aqData.mQueue,                                   // 4     aqData.mQueue,                                   // 4
- 
     NULL                                             // 5     NULL                                             // 5
- 
 ); );
- +// ユーザーインターフェーススレッドにおいてユーザーが録音を止めるまで待つ
-// Wait, on user interface thread, until user stops the recording +
 AudioQueueStop (                                     // 6 AudioQueueStop (                                     // 6
- 
     aqData.mQueue,                                   // 7     aqData.mQueue,                                   // 7
- 
     true                                             // 8     true                                             // 8
- 
 ); );
- 
-  
  
 aqData.mIsRunning = false;                           // 9 aqData.mIsRunning = false;                           // 9
 +</code>
  
-Here’s how this code works:+コードの働きを見てみましょう:
  
-    Initializes the packet index to to begin recording at the start of the audio file.+  - Audio Fileの先頭から記録するためにパケットインデックスを''0''に初期化します。 
 +  - Audio Queueが実行中を表すフラグを独自構造体にセットします。このフラグは録音Audio Queueコールバックで使用されます。 
 +  - ''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html#//apple_ref/doc/c_ref/AudioQueueStart|AudioQueueStart]]''関数は、Audio Queue自身のスレッドでAudio Queueを開始します。 
 +  - 開始するAudio Queue。 
 +  - ''NULL''を使うと、Audio Queueが直ちに録音を開始する事を示します。 
 +  - ''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html#//apple_ref/doc/c_ref/AudioQueueStop|AudioQueueStop]]''関数は録音Audio Queueを停止しリセットします。 
 +  - 停止するAudio Queue。 
 +  - ''true''を使うと、同期的に止めます。停止における同期と非同期の説明は“[[0200_aboutaudioqueues#Audio Queueの状態と制御]]”をご覧ください。 
 +  - Audio Queueが非実行中を示すフラグを独自構造体にセットします。
  
-    Sets a flag in the custom structure to indicate that the audio queue is running. This flag is used by the recording audio queue callback.+===== 録音後の後始末 =====
  
-    The AudioQueueStart function starts the audio queue, on its own thread. +録音が終わったら、Audio Queueを破棄しAudio Fileを閉じます。 
- +リスト 2-15はこれらの手順を示します。
-    The audio queue to start. +
- +
-    Uses NULL to indicate that the audio queue should start recording immediately. +
- +
-    The AudioQueueStop function stops and resets the recording audio queue. +
- +
-    The audio queue to stop. +
- +
-    Use true to use synchronous stopping. See “Audio Queue Control and State” for an explanation of synchronous and asynchronous stopping. +
- +
-    Sets a flag in the custom structure to indicate that the audio queue is not running. +
- +
-Clean Up After Recording +
- +
-When you’re finished with recording, dispose of the audio queue and close the audio file. Listing 2-15 illustrates these steps. +
- +
-Listing 2-15  Cleaning up after recording+
  
 +**リスト 2-15** 録音後の後始末
 +<code c_mac>
 AudioQueueDispose (                                 // 1 AudioQueueDispose (                                 // 1
- 
     aqData.mQueue,                                  // 2     aqData.mQueue,                                  // 2
- 
     true                                            // 3     true                                            // 3
- 
 ); );
- 
-  
  
 AudioFileClose (aqData.mAudioFile);                 // 4 AudioFileClose (aqData.mAudioFile);                 // 4
 +</code>
  
-Here’s how this code works: +コードの働きを見てみましょう:
- +
-    The AudioQueueDispose function disposes of the audio queue and all of its resources, including its buffers. +
- +
-    The audio queue you want to dispose of.+
  
-    Use true to dispose of the audio queue synchronously (that is, immediately).+  - ''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioQueueReference/Reference/reference.html#//apple_ref/doc/c_ref/AudioQueueDispose|AudioQueueDispose]]''関数はAudio Queueと、バッファを含むその全ての資源を破棄します。 
 +  - 破棄したいAudio Queue。 
 +  - ''true''を使うとAudio Queueを同期的に(すなわち、即座に)破棄します 
 +  - 記録に使ったAudio Fileを閉じます。''[[https://developer.apple.com/library/mac/documentation/MusicAudio/Reference/AudioFileConvertRef/Reference/reference.html#//apple_ref/doc/c_ref/AudioFileClose|AudioFileClose]]''関数は''AudioFile.h''ヘッダファイルで宣言されています。
  
-    Closes the audio file that was used for recording. The AudioFileClose function is declared in the AudioFile.h header file. 
  • translation/adc/audio/audio_queue_services_programming_guide/0300_recordingaudio.1409545031.txt.gz
  • 最終更新: 2014-09-01 13:17
  • by Decomo