Skip to content

Commit 8b299ae

Browse files
authored
Set channel layout for source and output formats (sbooth#466)
For more than two channels `AVAudioFormat` initialization fails without a channel layout Fixes sbooth#461
1 parent 16cfab2 commit 8b299ae

21 files changed

+32
-32
lines changed

Sources/CSFBAudioEngine/Decoders/SFBDSDIFFDecoder.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ - (BOOL)openReturningError:(NSError **)error
804804
sourceStreamDescription.mChannelsPerFrame = channelsChunk->mNumberChannels;
805805
sourceStreamDescription.mBitsPerChannel = 1;
806806

807-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
807+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
808808

809809
auto soundDataChunk = std::static_pointer_cast<DSDSoundDataChunk>(chunks->mLocalChunks['DSD ']);
810810
if(!soundDataChunk) {

Sources/CSFBAudioEngine/Decoders/SFBDSFDecoder.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ - (BOOL)openReturningError:(NSError **)error
288288
sourceStreamDescription.mChannelsPerFrame = (UInt32)channelNum;
289289
sourceStreamDescription.mBitsPerChannel = 1;
290290

291-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
291+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
292292

293293
// Metadata chunk is ignored
294294

Sources/CSFBAudioEngine/Decoders/SFBFLACDecoder.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ - (BOOL)openReturningError:(NSError **)error
331331

332332
sourceStreamDescription.mFramesPerPacket = _streamInfo.max_blocksize;
333333

334-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
334+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
335335

336336
// Populate codec properties
337337
_properties = @{

Sources/CSFBAudioEngine/Decoders/SFBLibsndfileDecoder.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2011-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2011-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -388,7 +388,7 @@ - (BOOL)openReturningError:(NSError **)error
388388
break;
389389
}
390390

391-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
391+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
392392

393393
return YES;
394394
}

Sources/CSFBAudioEngine/Decoders/SFBMPEGDecoder.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2006-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2006-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -239,7 +239,7 @@ - (BOOL)openReturningError:(NSError **)error
239239

240240
sourceStreamDescription.mFramesPerPacket = framesPerMPEGFrame;
241241

242-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
242+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
243243

244244
if(mpg123_scan(_mpg123) != MPG123_OK) {
245245
mpg123_close(_mpg123);

Sources/CSFBAudioEngine/Decoders/SFBModuleDecoder.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2011-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2011-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -144,8 +144,8 @@ - (BOOL)openReturningError:(NSError **)error
144144
return NO;
145145

146146
// Generate interleaved 2-channel 16-bit output
147-
AVAudioChannelLayout *layout = [[AVAudioChannelLayout alloc] initWithLayoutTag:kAudioChannelLayoutTag_Stereo];
148-
_processingFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatInt16 sampleRate:DUMB_SAMPLE_RATE interleaved:YES channelLayout:layout];
147+
// For mono and stereo the channel layout is assumed
148+
_processingFormat = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatInt16 sampleRate:DUMB_SAMPLE_RATE channels:DUMB_CHANNELS interleaved:YES];
149149

150150
// Set up the source format
151151
AudioStreamBasicDescription sourceStreamDescription = {0};

Sources/CSFBAudioEngine/Decoders/SFBMonkeysAudioDecoder.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ - (BOOL)openReturningError:(NSError **)error
284284
sourceStreamDescription.mSampleRate = _decompressor->GetInfo(APE::IAPEDecompress::APE_INFO_SAMPLE_RATE);
285285
sourceStreamDescription.mChannelsPerFrame = static_cast<UInt32>(_decompressor->GetInfo(APE::IAPEDecompress::APE_INFO_CHANNELS));
286286

287-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
287+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
288288

289289
// Populate codec properties
290290
_properties = @{

Sources/CSFBAudioEngine/Decoders/SFBMusepackDecoder.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2006-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2006-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -206,7 +206,7 @@ - (BOOL)openReturningError:(NSError **)error
206206

207207
sourceStreamDescription.mFramesPerPacket = (1 << streaminfo.block_pwr);
208208

209-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
209+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
210210

211211
// Populate codec properties
212212
_properties = @{

Sources/CSFBAudioEngine/Decoders/SFBOggOpusDecoder.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2006-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2006-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -196,7 +196,7 @@ - (BOOL)openReturningError:(NSError **)error
196196
sourceStreamDescription.mSampleRate = header->input_sample_rate;
197197
sourceStreamDescription.mChannelsPerFrame = (UInt32)header->channel_count;
198198

199-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
199+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
200200

201201
// Populate codec properties
202202
_properties = @{

Sources/CSFBAudioEngine/Decoders/SFBOggSpeexDecoder.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2011-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2011-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//

Sources/CSFBAudioEngine/Decoders/SFBOggVorbisDecoder.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2006-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2006-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -200,7 +200,7 @@ - (BOOL)openReturningError:(NSError **)error
200200
sourceStreamDescription.mSampleRate = ovInfo->rate;
201201
sourceStreamDescription.mChannelsPerFrame = (UInt32)ovInfo->channels;
202202

203-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
203+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
204204

205205
// Populate codec properties
206206
_properties = @{

Sources/CSFBAudioEngine/Decoders/SFBShortenDecoder.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ - (BOOL)openReturningError:(NSError **)error
562562

563563
sourceStreamDescription.mFramesPerPacket = static_cast<UInt32>(_blocksize);
564564

565-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
565+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
566566

567567
// Populate codec properties
568568
_properties = @{

Sources/CSFBAudioEngine/Decoders/SFBTrueAudioDecoder.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ - (BOOL)openReturningError:(NSError **)error
212212
sourceStreamDescription.mChannelsPerFrame = streamInfo.nch;
213213
sourceStreamDescription.mBitsPerChannel = streamInfo.bps;
214214

215-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
215+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
216216

217217
// Populate codec properties
218218
_properties = @{

Sources/CSFBAudioEngine/Decoders/SFBWavPackDecoder.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2011-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2011-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -345,7 +345,7 @@ - (BOOL)openReturningError:(NSError **)error
345345
sourceStreamDescription.mBitsPerChannel = (UInt32)WavpackGetBitsPerSample(_wpc);
346346
sourceStreamDescription.mBytesPerPacket = (UInt32)WavpackGetBytesPerSample(_wpc);
347347

348-
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription];
348+
_sourceFormat = [[AVAudioFormat alloc] initWithStreamDescription:&sourceStreamDescription channelLayout:channelLayout];
349349

350350
// Populate codec properties
351351
_properties = @{

Sources/CSFBAudioEngine/Encoders/SFBCoreAudioEncoder.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ - (BOOL)openReturningError:(NSError **)error
411411
default: format.mFormatFlags = kAppleLosslessFormatFlag_16BitSourceData; break;
412412
}
413413
}
414-
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&format];
414+
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&format channelLayout:_processingFormat.channelLayout];
415415

416416
AudioFileID audioFile;
417417
auto result = AudioFileInitializeWithCallbacks((__bridge void *)self, my_AudioFile_ReadProc, my_AudioFile_WriteProc, my_AudioFile_GetSizeProc, my_AudioFile_SetSizeProc, fileType, &format, 0, &audioFile);

Sources/CSFBAudioEngine/Encoders/SFBFLACEncoder.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ - (BOOL)openReturningError:(NSError **)error
325325
break;
326326
}
327327
outputStreamDescription.mFramesPerPacket = FLAC__stream_encoder_get_blocksize(flac.get());
328-
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription];
328+
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription channelLayout:_processingFormat.channelLayout];
329329

330330
_flac = std::move(flac);
331331
_seektable = std::move(seektable);

Sources/CSFBAudioEngine/Encoders/SFBMonkeysAudioEncoder.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ - (BOOL)openReturningError:(NSError **)error
309309
outputStreamDescription.mBitsPerChannel = wve.wBitsPerSample;
310310
outputStreamDescription.mSampleRate = wve.nSamplesPerSec;
311311
outputStreamDescription.mChannelsPerFrame = wve.nChannels;
312-
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription];
312+
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription channelLayout:_processingFormat.channelLayout];
313313

314314
return YES;
315315
}

Sources/CSFBAudioEngine/Encoders/SFBOggOpusEncoder.mm

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2020-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2020-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -309,7 +309,7 @@ - (BOOL)openReturningError:(NSError **)error
309309
outputStreamDescription.mFormatID = kAudioFormatOpus;
310310
outputStreamDescription.mSampleRate = _processingFormat.sampleRate;
311311
outputStreamDescription.mChannelsPerFrame = _processingFormat.channelCount;
312-
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription];
312+
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription channelLayout:_processingFormat.channelLayout];
313313

314314
_enc = std::move(enc);
315315
_comments = std::move(comments);

Sources/CSFBAudioEngine/Encoders/SFBOggVorbisEncoder.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2020-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2020-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -196,7 +196,7 @@ - (BOOL)openReturningError:(NSError **)error
196196
outputStreamDescription.mFormatID = kSFBAudioFormatVorbis;
197197
outputStreamDescription.mSampleRate = _processingFormat.sampleRate;
198198
outputStreamDescription.mChannelsPerFrame = _processingFormat.channelCount;
199-
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription];
199+
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription channelLayout:_processingFormat.channelLayout];
200200

201201
_isOpen = YES;
202202

Sources/CSFBAudioEngine/Encoders/SFBTrueAudioEncoder.mm

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ - (BOOL)openReturningError:(NSError **)error
160160
outputStreamDescription.mBitsPerChannel = _processingFormat.streamDescription->mBitsPerChannel;
161161
outputStreamDescription.mSampleRate = _processingFormat.sampleRate;
162162
outputStreamDescription.mChannelsPerFrame = _processingFormat.channelCount;
163-
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription];
163+
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription channelLayout:_processingFormat.channelLayout];
164164

165165
return YES;
166166
}

Sources/CSFBAudioEngine/Encoders/SFBWavPackEncoder.m

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2020-2024 Stephen F. Booth <me@sbooth.org>
2+
// Copyright (c) 2020-2025 Stephen F. Booth <me@sbooth.org>
33
// Part of https://github.com/sbooth/SFBAudioEngine
44
// MIT license
55
//
@@ -195,7 +195,7 @@ - (BOOL)openReturningError:(NSError **)error
195195
outputStreamDescription.mBitsPerChannel = _processingFormat.streamDescription->mBitsPerChannel;
196196
outputStreamDescription.mSampleRate = _processingFormat.sampleRate;
197197
outputStreamDescription.mChannelsPerFrame = _processingFormat.channelCount;
198-
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription];
198+
_outputFormat = [[AVAudioFormat alloc] initWithStreamDescription:&outputStreamDescription channelLayout:_processingFormat.channelLayout];
199199

200200
return YES;
201201
}

0 commit comments

Comments
 (0)