Debian Codegraph @

modules/lsp-runtime-lib/src/main/mm/OutAudioFileStream.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 20 апр. 2020 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/mm/OutAudioFileStream.h>
23
24 #ifdef PLATFORM_WINDOWS
25 #include <private/mm/ACMStream.h>
26 #include <private/mm/MMIOWriter.h>
27
28 #include <windows.h>
29 #include <mmsystem.h>
30 #include <mmreg.h>
31 #include <msacm.h>
32 #endif /* PLATFORM_WINDOWS */
33
34 #if defined(PLATFORM_MACOSX)
35 #include <CoreFoundation/CFString.h>
36 #include <CoreFoundation/CFURL.h>
37 #include <AudioToolbox/ExtendedAudioFile.h>
38 #endif /* PLATFORM_MACOSX */
39
40 #ifdef USE_LIBSNDFILE
41 #if (__SIZEOF_INT__ == 4)
42 #define AFS_S32_CPU mm::SFMT_S32_CPU
43 #endif
44
45 #if (__SIZEOF_SHORT__ == 2)
46 #define AFS_S16_CPU mm::SFMT_S16_CPU
47 #elif ((__SIZEOF_SHORT__ == 4) && (!defined(AFS_S32_CPU)))
48 #define AFS_S32_CPU mm::SFMT_S32_CPU
49 #endif
50 #endif /* USE_LIBSNDFILE */
51
52 namespace lsp
53 {
54 namespace mm
55 {
56 #if defined(PLATFORM_WINDOWS)
57 struct WAVEFILE
58 {
59 MMIOWriter *pMMIO; // MMIO writer
60 ACMStream *pACM; // ACM stream
61 WAVEFORMATEX *pFormat; // Actual PCM stream format
62 wsize_t nTotalFrames; // Total frames written
63 WAVEFORMATEX sPcmFmt; // PCM format descriptor
64 };
65
66 static ssize_t decode_sample_format(WAVEFORMATEX *wfe)
67 {
68 if (wfe->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
69 return SFMT_F32_LE;
70
71 if (wfe->wFormatTag == WAVE_FORMAT_PCM)
72 {
73 switch (wfe->wBitsPerSample)
74 {
75 case 8: return SFMT_U8_CPU;
76 case 16: return SFMT_S16_LE;
77 case 24: return SFMT_S24_LE;
78 case 32: return SFMT_S32_LE;
79 default:
80 break;
81 }
82 }
83
84 return -1;
85 }
86 #elif defined(PLATFORM_MACOSX)
87 static inline ExtAudioFileRef eaf_ref(void *handle)
88 {
89 return static_cast<ExtAudioFileRef>(handle);
90 }
91
92 status_t OutAudioFileStream::decode_os_status(uint32_t code)
93 {
94 return (code == kAudio_NoError) ? STATUS_OK : STATUS_UNKNOWN_ERR;
95 }
96
97 uint32_t OutAudioFileStream::select_file_format(size_t codec)
98 {
99 // Choose audio format
100 switch (codec & AFMT_MASK)
101 {
102 case AFMT_WAV: return kAudioFileWAVEType;
103 case AFMT_AIFF: return kAudioFileAIFFType;
104 case AFMT_AU: return kAudioFileNextType;
105 case AFMT_W64: return kAudioFileWave64Type;
106 case AFMT_SD2: return kAudioFileSoundDesigner2Type;
107 case AFMT_FLAC: return kAudioFileFLACType;
108 case AFMT_CAF: return kAudioFileCAFType;
109 case AFMT_RF64: return kAudioFileRF64Type;
110 default:
111 return 0;
112 }
113 }
114
115 static bool select_sample_format(AudioStreamBasicDescription *info, const audio_stream_t *fmt, size_t codec)
116 {
117 bzero(info, sizeof(AudioStreamBasicDescription));
118
119 info->mSampleRate = fmt->srate;
120 info->mFormatFlags = 0;
121 info->mChannelsPerFrame = uint32_t(fmt->channels);
122
123 // Choose codec
124 switch (codec & CFMT_MASK)
125 {
126 case CFMT_PCM:
127 info->mFormatID = kAudioFormatLinearPCM;
128 break;
129
130 case CFMT_ULAW:
131 info->mFormatID = kAudioFormatULaw;
132 info->mFormatFlags |= kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
133 info->mBytesPerPacket = 0;
134 info->mFramesPerPacket = 1;
135 info->mBytesPerFrame = uint32_t(8 * sizeof(int8_t) * fmt->channels);
136 info->mBitsPerChannel = 8 * sizeof(int8_t);
137 break;
138
139 case CFMT_ALAW:
140 info->mFormatID = kAudioFormatALaw;
141 info->mFormatFlags |= kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;
142 info->mBytesPerPacket = 0;
143 info->mFramesPerPacket = 1;
144 info->mBytesPerFrame = uint32_t(8 * sizeof(int8_t) * fmt->channels);
145 info->mBitsPerChannel = 8 * sizeof(int8_t);
146 break;
147
148 default:
149 switch (codec & AFMT_MASK)
150 {
151 case AFMT_WAV:
152 info->mFormatID = kAudioFormatLinearPCM;
153 break;
154 case AFMT_FLAC:
155 info->mFormatID = kAudioFormatFLAC;
156 break;
157 default:
158 info->mFormatID = kAudioFormatLinearPCM;
159 break;
160 }
161 break;
162 }
163
164 // Choose endianess
165 switch (sformat_endian(fmt->format))
166 {
167 case SFMT_DFL:
168 if ((codec & AFMT_MASK) == AFMT_AU)
169 info->mFormatFlags |= kAudioFormatFlagIsBigEndian;
170 break;
171 case SFMT_LE:
172 break;
173 case SFMT_BE:
174 info->mFormatFlags |= kAudioFormatFlagIsBigEndian;
175 break;
176 default:
177 return false;
178 }
179
180 if (info->mFormatID == kAudioFormatLinearPCM)
181 {
182 info->mFormatFlags |= kAudioFormatFlagIsPacked;
183
184 // Set sample format
185 switch (sformat_format(fmt->format))
186 {
187 case SFMT_U8:
188 info->mBytesPerPacket = uint32_t(sizeof(uint8_t) * fmt->channels);
189 info->mFramesPerPacket = 1;
190 info->mBytesPerFrame = info->mBytesPerPacket;
191 info->mBitsPerChannel = 8 * sizeof(uint8_t);
192 break;
193
194 case SFMT_S8:
195 info->mFormatFlags |= kAudioFormatFlagIsSignedInteger;
196 info->mBytesPerPacket = uint32_t(sizeof(int8_t) * fmt->channels);
197 info->mFramesPerPacket = 1;
198 info->mBytesPerFrame = info->mBytesPerPacket;
199 info->mBitsPerChannel = 8 * sizeof(int8_t);
200 break;
201
202 case SFMT_U16:
203 info->mBytesPerPacket = uint32_t(sizeof(uint16_t) * fmt->channels);
204 info->mFramesPerPacket = 1;
205 info->mBytesPerFrame = info->mBytesPerPacket;
206 info->mBitsPerChannel = 8 * sizeof(uint16_t);
207 break;
208
209 case SFMT_S16:
210 info->mFormatFlags |= kAudioFormatFlagIsSignedInteger;
211 info->mBytesPerPacket = uint32_t(sizeof(int16_t) * fmt->channels);
212 info->mFramesPerPacket = 1;
213 info->mBytesPerFrame = info->mBytesPerPacket;
214 info->mBitsPerChannel = 8 * sizeof(int16_t);
215 break;
216
217 case SFMT_U24:
218 info->mBytesPerPacket = uint32_t(3 * sizeof(uint8_t) * fmt->channels);
219 info->mFramesPerPacket = 1;
220 info->mFramesPerPacket = 1;
221 info->mBytesPerFrame = info->mBytesPerPacket;
222 info->mBitsPerChannel = 24 * sizeof(uint8_t);
223 break;
224
225 case SFMT_S24:
226 info->mFormatFlags |= kAudioFormatFlagIsSignedInteger;
227 info->mBytesPerPacket = uint32_t(3 * sizeof(int8_t) * fmt->channels);
228 info->mFramesPerPacket = 1;
229 info->mBytesPerFrame = info->mBytesPerPacket;
230 info->mBitsPerChannel = 24 * sizeof(int8_t);
231 break;
232
233 case SFMT_U32:
234 info->mBytesPerPacket = uint32_t(sizeof(uint32_t) * fmt->channels);
235 info->mFramesPerPacket = 1;
236 info->mBytesPerFrame = info->mBytesPerPacket;
237 info->mBitsPerChannel = 8 * sizeof(uint32_t);
238 break;
239
240 case SFMT_S32:
241 info->mFormatFlags |= kAudioFormatFlagIsSignedInteger;
242 info->mBytesPerPacket = uint32_t(sizeof(int32_t) * fmt->channels);
243 info->mFramesPerPacket = 1;
244 info->mBytesPerFrame = info->mBytesPerPacket;
245 info->mBitsPerChannel = 8 * sizeof(int32_t);
246 break;
247
248 case SFMT_F32:
249 info->mFormatFlags |= kAudioFormatFlagIsFloat;
250 info->mBytesPerPacket = uint32_t(sizeof(f32_t) * fmt->channels);
251 info->mFramesPerPacket = 1;
252 info->mBytesPerFrame = info->mBytesPerPacket;
253 info->mBitsPerChannel = 8 * sizeof(f32_t);
254 break;
255
256 case SFMT_F64:
257 info->mFormatFlags |= kAudioFormatFlagIsFloat;
258 info->mBytesPerPacket = uint32_t(sizeof(f64_t) * fmt->channels);
259 info->mFramesPerPacket = 1;
260 info->mBytesPerFrame = info->mBytesPerPacket;
261 info->mBitsPerChannel = 8 * sizeof(f64_t);
262 break;
263
264 default:
265 return false;
266 }
267 }
268
269 return true;
270 }
271 #else
272 status_t OutAudioFileStream::decode_sf_error(SNDFILE *fd)
273 {
274 switch (sf_error(fd))
275 {
276 case SF_ERR_NO_ERROR:
277 return STATUS_OK;
278 case SF_ERR_UNRECOGNISED_FORMAT:
279 return STATUS_BAD_FORMAT;
280 case SF_ERR_MALFORMED_FILE:
281 return STATUS_CORRUPTED_FILE;
282 case SF_ERR_UNSUPPORTED_ENCODING:
283 return STATUS_BAD_FORMAT;
284 default:
285 return STATUS_UNKNOWN_ERR;
286 }
287 }
288
289 bool OutAudioFileStream::select_sndfile_format(SF_INFO *info, audio_stream_t *fmt, size_t codec)
290 {
291 int res = 0;
292
293 // Choose audio format
294 switch (codec & AFMT_MASK)
295 {
296 case AFMT_WAV: res |= SF_FORMAT_WAV; break;
297 case AFMT_AIFF: res |= SF_FORMAT_AIFF; break;
298 case AFMT_AU: res |= SF_FORMAT_AU; break;
299 case AFMT_RAW: res |= SF_FORMAT_RAW; break;
300 case AFMT_PAF: res |= SF_FORMAT_PAF; break;
301 case AFMT_SVX: res |= SF_FORMAT_SVX; break;
302 case AFMT_NIST: res |= SF_FORMAT_NIST; break;
303 case AFMT_VOC: res |= SF_FORMAT_VOC; break;
304 case AFMT_IRCAM: res |= SF_FORMAT_IRCAM; break;
305 case AFMT_W64: res |= SF_FORMAT_W64; break;
306 case AFMT_MAT4: res |= SF_FORMAT_MAT4; break;
307 case AFMT_MAT5: res |= SF_FORMAT_MAT5; break;
308 case AFMT_PVF: res |= SF_FORMAT_PVF; break;
309 case AFMT_XI: res |= SF_FORMAT_XI; break;
310 case AFMT_HTK: res |= SF_FORMAT_HTK; break;
311 case AFMT_SDS: res |= SF_FORMAT_SDS; break;
312 case AFMT_AVR: res |= SF_FORMAT_AVR; break;
313 case AFMT_WAVEX: res |= SF_FORMAT_WAVEX; break;
314 case AFMT_SD2: res |= SF_FORMAT_SD2; break;
315 case AFMT_FLAC: res |= SF_FORMAT_FLAC; break;
316 case AFMT_CAF: res |= SF_FORMAT_CAF; break;
317 case AFMT_WVE: res |= SF_FORMAT_WVE; break;
318 case AFMT_OGG: res |= SF_FORMAT_OGG; break;
319 case AFMT_MPC2K: res |= SF_FORMAT_MPC2K; break;
320 case AFMT_RF64: res |= SF_FORMAT_RF64; break;
321 default:
322 return false;
323 }
324
325 // Choose codec
326 switch (codec & CFMT_MASK)
327 {
328 case CFMT_PCM:
329 {
330 // Set sample format
331 switch (sformat_format(fmt->format))
332 {
333 case SFMT_U8: res |= SF_FORMAT_PCM_U8; break;
334 case SFMT_S8: res |= SF_FORMAT_PCM_S8; break;
335 case SFMT_U16:
336 case SFMT_S16:
337 res |= SF_FORMAT_PCM_16;
338 break;
339 case SFMT_S24:
340 case SFMT_U24:
341 res |= SF_FORMAT_PCM_24;
342 break;
343 case SFMT_S32:
344 case SFMT_U32:
345 res |= SF_FORMAT_PCM_32;
346 break;
347
348 case SFMT_F32: res |= SF_FORMAT_FLOAT; break;
349 case SFMT_F64: res |= SF_FORMAT_DOUBLE; break;
350 default:
351 return false;
352 }
353 }
354 break;
355
356 case CFMT_ULAW: res |= SF_FORMAT_ULAW; break;
357 case CFMT_ALAW: res |= SF_FORMAT_ALAW; break;
358 case CFMT_IMA_ADPCM: res |= SF_FORMAT_IMA_ADPCM; break;
359 case CFMT_MS_ADPCM: res |= SF_FORMAT_MS_ADPCM; break;
360 case CFMT_GSM610: res |= SF_FORMAT_GSM610; break;
361 case CFMT_VOX_ADPCM: res |= SF_FORMAT_VOX_ADPCM; break;
362 case CFMT_G721_32: res |= SF_FORMAT_G721_32; break;
363 case CFMT_G723_24: res |= SF_FORMAT_G723_24; break;
364 case CFMT_G723_40: res |= SF_FORMAT_G723_40; break;
365 case CFMT_DWVW_12: res |= SF_FORMAT_DWVW_12; break;
366 case CFMT_DWVW_16: res |= SF_FORMAT_DWVW_16; break;
367 case CFMT_DWVW_24: res |= SF_FORMAT_DWVW_24; break;
368 case CFMT_DWVW_N: res |= SF_FORMAT_DWVW_N; break;
369 case CFMT_DPCM_8: res |= SF_FORMAT_DPCM_8; break;
370 case CFMT_DPCM_16: res |= SF_FORMAT_DPCM_16; break;
371 case CFMT_VORBIS: res |= SF_FORMAT_VORBIS; break;
372 #ifdef LIBSNDFILE_HAS_ALAC
373 case CFMT_ALAC_16: res |= SF_FORMAT_ALAC_16; break;
374 case CFMT_ALAC_20: res |= SF_FORMAT_ALAC_20; break;
375 case CFMT_ALAC_24: res |= SF_FORMAT_ALAC_24; break;
376 case CFMT_ALAC_32: res |= SF_FORMAT_ALAC_32; break;
377 #endif /* LIBSNDFILE_HAS_ALAC */
378
379 default:
380 return false;
381 }
382
383 // Choose endianess
384 switch (sformat_endian(fmt->format))
385 {
386 case SFMT_DFL: res |= SF_ENDIAN_FILE; break;
387 case SFMT_LE: res |= SF_ENDIAN_LITTLE; break;
388 case SFMT_BE: res |= SF_ENDIAN_BIG; break;
389 default:
390 return false;
391 }
392
393 info->frames = fmt->frames;
394 info->samplerate = fmt->srate;
395 info->channels = fmt->channels;
396 info->format = res;
397 info->sections = 0;
398 info->seekable = 0;
399
400 return true;
401 }
402 #endif
403
404 OutAudioFileStream::OutAudioFileStream()
405 {
406 hHandle = NULL;
407 nCodec = 0;
408 bSeekable = false;
409 }
410
411 OutAudioFileStream::~OutAudioFileStream()
412 {
413 IOutAudioStream::close();
414 do_close();
415 }
416
417 status_t OutAudioFileStream::open(const char *path, const audio_stream_t *fmt, size_t codec)
418 {
419 if (path == NULL)
420 return set_error(STATUS_BAD_ARGUMENTS);
421 LSPString xpath;
422 if (!xpath.set_utf8(path))
423 return set_error(STATUS_NO_MEM);
424 return open(&xpath, fmt, codec);
425 }
426
427 status_t OutAudioFileStream::open(const io::Path *path, const audio_stream_t *fmt, size_t codec)
428 {
429 if (path == NULL)
430 return set_error(STATUS_BAD_ARGUMENTS);
431 return open(path->as_string(), fmt, codec);
432 }
433
434 status_t OutAudioFileStream::open(const LSPString *path, const audio_stream_t *fmt, size_t codec)
435 {
436 if (!is_closed())
437 return set_error(STATUS_OPENED);
438 if (fmt == NULL)
439 return set_error(STATUS_BAD_ARGUMENTS);
440
441 #if defined(PLATFORM_WINDOWS)
442 if ((codec & AFMT_MASK) != AFMT_WAV)
443 return set_error(STATUS_UNSUPPORTED_FORMAT);
444
445 WAVEFORMATEX rfmt;
446
447 ::ZeroMemory(&rfmt, sizeof(WAVEFORMATEX));
448 rfmt.cbSize = 0;
449 rfmt.nChannels = fmt->channels;
450 rfmt.nSamplesPerSec = fmt->srate;
451 rfmt.wBitsPerSample = sformat_size_of(fmt->format) * 8;
452 rfmt.nBlockAlign = fmt->channels * sformat_size_of(fmt->format);
453 rfmt.nAvgBytesPerSec = fmt->srate * rfmt.nBlockAlign;
454
455 switch (codec & CFMT_MASK)
456 {
457 case CFMT_PCM:
458 rfmt.wFormatTag = (sformat_format(fmt->format) == SFMT_F32) ?
459 WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
460 break;
461
462 case CFMT_ULAW: rfmt.wFormatTag = WAVE_FORMAT_MULAW; break;
463 case CFMT_ALAW: rfmt.wFormatTag = WAVE_FORMAT_ALAW; break;
464 case CFMT_MS_ADPCM: rfmt.wFormatTag = WAVE_FORMAT_ADPCM; break;
465 case CFMT_IMA_ADPCM: rfmt.wFormatTag = WAVE_FORMAT_IMA_ADPCM; break;
466
467 case CFMT_GSM610: rfmt.wFormatTag = WAVE_FORMAT_GSM610; break;
468
469 case CFMT_G721_32: rfmt.wFormatTag = WAVE_FORMAT_G721_ADPCM; break;
470 case CFMT_G723_24: rfmt.wFormatTag = WAVE_FORMAT_G723_ADPCM; break;
471 case CFMT_G723_40: rfmt.wFormatTag = WAVE_FORMAT_G723_ADPCM; break;
472
473 default:
474 return set_error(STATUS_UNSUPPORTED_FORMAT);
475 }
476
477 // Allocate handle
478 handle_t *h = static_cast<handle_t *>(malloc(sizeof(handle_t)));
479 if (h == NULL)
480 return set_error(STATUS_NO_MEM);
481 h->pMMIO = NULL;
482 h->pACM = NULL;
483 h->pFormat = NULL;
484 h->nTotalFrames = 0;
485 lsp_finally { close_handle(h); };
486
487 // Create MMIO
488 status_t res;
489 h->pMMIO = new MMIOWriter();
490 if (h->pMMIO == NULL)
491 return set_error(STATUS_NO_MEM);
492
493 if ((rfmt.wFormatTag == WAVE_FORMAT_IEEE_FLOAT) ||
494 (rfmt.wFormatTag == WAVE_FORMAT_PCM))
495 {
496 // Just open MMIO stream with specified format
497 if ((res = h->pMMIO->open(path, &rfmt, fmt->frames)) != STATUS_OK)
498 return set_error(res);
499
500 // Update state
501 h->nTotalFrames = 0;
502 h->pACM = NULL;
503 h->sPcmFmt = rfmt;
504 h->pFormat = &h->sPcmFmt;
505
506 sFormat = *fmt;
507
508 nCodec = codec;
509 nOffset = 0;
510 bSeekable = h->pMMIO->seekable();
511
512 // Commit the new handle and return success
513 lsp::swap(hHandle, h);
514 return set_error(STATUS_OK);
515 }
516
517 // Create ACM stream first and initialize it
518 h->pACM = new ACMStream();
519 if (h->pACM == NULL)
520 return set_error(STATUS_NO_MEM);
521 if ((res = h->pACM->write_pcm(&rfmt)) != STATUS_OK)
522 return set_error(res);
523
524 // Detect format
525 h->pFormat = h->pACM->in_format();
526 ssize_t sfmt = decode_sample_format(h->pFormat);
527 if (sfmt <= 0)
528 return set_error(STATUS_UNSUPPORTED_FORMAT);
529
530 // Now open MMIO with specified output format
531 if ((res = h->pMMIO->open(path, h->pACM->out_format(), fmt->frames)) != STATUS_OK)
532 return set_error(res);
533
534 // All is OK, update state
535 h->nTotalFrames = 0;
536
537 sFormat.srate = h->pFormat->nSamplesPerSec;
538 sFormat.channels = h->pFormat->nChannels;
539 sFormat.frames = fmt->frames;
540 sFormat.format = sfmt;
541
542 nCodec = codec;
543 nOffset = 0;
544 bSeekable = false;
545
546 // Commit the new handle and return success
547 lsp::swap(hHandle, h);
548 return set_error(STATUS_OK);
549 #elif defined(PLATFORM_MACOSX)
550 // Form the URL to open
551 CFStringRef str_ref = CFStringCreateWithCString(
552 kCFAllocatorDefault,
553 path->get_utf8(),
554 kCFStringEncodingUTF8);
555 lsp_finally { CFRelease(str_ref); };
556 CFURLRef url_ref = CFURLCreateWithFileSystemPath(
557 kCFAllocatorDefault,
558 str_ref,
559 kCFURLPOSIXPathStyle,
560 false);
561 lsp_finally { CFRelease(url_ref); };
562
563 const AudioFileTypeID file_type = select_file_format(codec);
564 if (file_type == 0)
565 return set_error(STATUS_UNSUPPORTED_FORMAT);
566
567 AudioStreamBasicDescription info;
568 if (!select_sample_format(&info, fmt, codec))
569 return set_error(STATUS_UNSUPPORTED_FORMAT);
570
571 // Open file
572 ExtAudioFileRef eaf = NULL;
573 OSStatus os_res = ExtAudioFileCreateWithURL(
574 url_ref,
575 file_type,
576 &info,
577 NULL,
578 kAudioFileFlags_EraseFile,
579 &eaf);
580 if (os_res != kAudio_NoError)
581 return set_error(decode_os_status(os_res));
582 lsp_finally {
583 if (eaf != NULL)
584 ExtAudioFileDispose(eaf);
585 };
586
587 // Decode sample format
588 size_t format = 0;
589 size_t be_flag = (info.mFormatFlags & kAudioFormatFlagIsBigEndian) ? mm::SFMT_BE : mm::SFMT_LE;
590 bool need_convert = info.mFormatID != kAudioFormatLinearPCM;
591 if (!need_convert)
592 {
593 if (info.mFormatFlags & kAudioFormatFlagIsFloat)
594 {
595 if (info.mBitsPerChannel == sizeof(float) * 8)
596 format = mm::SFMT_F32 | be_flag;
597 else if (info.mBitsPerChannel == sizeof(double) * 8)
598 format = mm::SFMT_F64 | be_flag;
599 else
600 need_convert = true;
601 }
602 else if (info.mFormatFlags & kAudioFormatFlagIsSignedInteger)
603 {
604 if (info.mBitsPerChannel == sizeof(int8_t) * 8)
605 format = mm::SFMT_S8 | be_flag;
606 else if (info.mBitsPerChannel == sizeof(int16_t) * 8)
607 format = mm::SFMT_S16 | be_flag;
608 else if (info.mBitsPerChannel == sizeof(int8_t) * 24)
609 format = mm::SFMT_S24 | be_flag;
610 else if (info.mBitsPerChannel == sizeof(int32_t) * 8)
611 format = mm::SFMT_S32 | be_flag;
612 else
613 need_convert = true;
614 }
615 else
616 {
617 if (info.mBitsPerChannel == sizeof(uint8_t) * 8)
618 format = mm::SFMT_U8 | be_flag;
619 else if (info.mBitsPerChannel == sizeof(uint16_t) * 8)
620 format = mm::SFMT_U16 | be_flag;
621 else if (info.mBitsPerChannel == sizeof(uint8_t) * 24)
622 format = mm::SFMT_U24 | be_flag;
623 else if (info.mBitsPerChannel == sizeof(uint32_t) * 8)
624 format = mm::SFMT_U32 | be_flag;
625 else
626 need_convert = true;
627 }
628 }
629
630 // Initialize audio converter if needed to match the selected sample format
631 if (need_convert)
632 {
633 AudioStreamBasicDescription cvt;
634 bzero(&cvt, sizeof(AudioStreamBasicDescription));
635 cvt.mSampleRate = info.mSampleRate;
636 cvt.mFormatID = kAudioFormatLinearPCM;
637 cvt.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
638
639 if (info.mBitsPerChannel <= sizeof(f32_t)*8)
640 {
641 cvt.mBytesPerPacket = sizeof(f32_t) * info.mChannelsPerFrame;
642 cvt.mFramesPerPacket = 1;
643 cvt.mBytesPerFrame = sizeof(f32_t) * info.mChannelsPerFrame;
644 cvt.mChannelsPerFrame = info.mChannelsPerFrame;
645 cvt.mBitsPerChannel = sizeof(f32_t) * 8;
646 format = mm::SFMT_F32 | be_flag;
647 }
648 else
649 {
650 cvt.mBytesPerPacket = sizeof(f64_t) * info.mChannelsPerFrame;
651 cvt.mFramesPerPacket = 1;
652 cvt.mBytesPerFrame = sizeof(f64_t) * info.mChannelsPerFrame;
653 cvt.mChannelsPerFrame = info.mChannelsPerFrame;
654 cvt.mBitsPerChannel = sizeof(f64_t) * 8;
655 format = mm::SFMT_F64 | be_flag;
656 }
657
658 os_res = ExtAudioFileSetProperty(
659 eaf,
660 kExtAudioFileProperty_ClientDataFormat,
661 sizeof(AudioStreamBasicDescription),
662 &cvt);
663
664 if (os_res != kAudio_NoError)
665 return set_error(decode_os_status(os_res));
666 }
667
668 // Update state and return
669 sFormat.srate = info.mSampleRate;
670 sFormat.channels = info.mChannelsPerFrame;
671 sFormat.frames = fmt->frames;
672 sFormat.format = format;
673
674 nOffset = 0;
675 bSeekable = false;
676 hHandle = release_ptr(eaf);
677
678 return set_error(STATUS_OK);
679 #else
680 audio_stream_t tmp;
681 SF_INFO info;
682 tmp = *fmt;
683
684 if (!select_sndfile_format(&info, &tmp, codec))
685 return set_error(STATUS_UNSUPPORTED_FORMAT);
686
687 // Open handle
688 SNDFILE *sf = sf_open(path->get_native(), SFM_WRITE, &info);
689 if (sf == NULL)
690 return -set_error(decode_sf_error(sf));
691
692 // Commit new state
693 sFormat = tmp;
694 hHandle = sf;
695 nOffset = 0;
696 bSeekable = info.seekable;
697
698 return set_error(STATUS_OK);
699 #endif
700 }
701
702 status_t OutAudioFileStream::close_handle(handle_t *h)
703 {
704 if (h == NULL)
705 return STATUS_OK;
706 lsp_finally { h = NULL; };
707
708 #if defined(PLATFORM_WINDOWS)
709 status_t res = STATUS_OK;
710
711 // Flush all previously encoded data and close ACM
712 if (h->pACM != NULL)
713 {
714 res = update_status(res, h->pACM->close());
715 delete h->pACM;
716 h->pACM = NULL;
717 }
718
719 // Close MMIO
720 if (h->pMMIO != NULL)
721 {
722 h->pMMIO->set_frames(h->nTotalFrames);
723 res = update_status(res, h->pMMIO->close());
724 delete h->pMMIO;
725 h->pMMIO = NULL;
726 }
727
728 h->pFormat = NULL;
729
730 free(h);
731
732 return res;
733 #elif defined(PLATFORM_MACOSX)
734 OSStatus res = ExtAudioFileDispose(eaf_ref(h));
735 return decode_os_status(res);
736 #else
737 int res = sf_close(h);
738 return (res == 0) ? STATUS_OK : STATUS_IO_ERROR;
739 #endif
740 }
741
742 status_t OutAudioFileStream::do_close()
743 {
744 if (hHandle == NULL)
745 return STATUS_OK;
746
747 status_t res = flush_internal(true);
748 status_t res2= close_handle(hHandle);
749
750 hHandle = NULL;
751 bSeekable = false;
752 nOffset = -1; // Mark as closed
753 nCodec = 0;
754
755 return set_error((res == STATUS_OK) ? res2 : res);
756 }
757
758 #if defined(PLATFORM_WINDOWS)
759 status_t OutAudioFileStream::flush_handle(handle_t *hHandle, bool eof)
760 {
761 if (hHandle->pACM == NULL)
762 return STATUS_OK;
763
764 void *dptr;
765
766 while (true)
767 {
768 // Perform pull of portion of buffer
769 ssize_t count = hHandle->pACM->pull(&dptr, IO_BUF_SIZE, eof);
770 if (count < 0)
771 return (count == -STATUS_EOF) ? STATUS_OK : -count;
772 else if (count == 0)
773 break;
774
775 // Write pulled data to underlying storage
776 count = hHandle->pMMIO->write(dptr, count);
777 if (count < 0)
778 return count;
779 }
780
781 return STATUS_OK;
782 }
783 #endif /* PLATFORM_WINDOWS */
784
785 status_t OutAudioFileStream::flush_internal(bool eof)
786 {
787 #if defined(PLATFORM_WINDOWS)
788 status_t res = flush_handle(hHandle, eof);
789
790 return (res == STATUS_OK) ? hHandle->pMMIO->flush() : res;
791 #elif defined(PLATFORM_MACOSX)
792 return STATUS_OK;
793 #else
794 sf_write_sync(hHandle);
795 return STATUS_OK;
796 #endif /* USE_LIBSNDFILE */
797 }
798
799 status_t OutAudioFileStream::flush()
800 {
801 if (is_closed())
802 return -set_error(STATUS_CLOSED);
803
804 return set_error(flush_internal(false));
805 }
806
807 status_t OutAudioFileStream::close()
808 {
809 IOutAudioStream::close();
810 return do_close();
811 }
812
813 ssize_t OutAudioFileStream::direct_write(const void *src, size_t nframes, size_t fmt)
814 {
815 #if defined(PLATFORM_WINDOWS)
816 size_t fsize = hHandle->pFormat->nBlockAlign;
817 if (hHandle->pMMIO != NULL)
818 {
819 if (hHandle->pACM != NULL)
820 return write_acm_convert(src, nframes);
821
822 ssize_t nwritten = hHandle->pMMIO->write(src, fsize * nframes);
823 return (nwritten < 0) ? nwritten : nwritten / fsize;
824 }
825
826 return -STATUS_NOT_SUPPORTED;
827 #elif defined(PLATFORM_MACOSX)
828 const size_t fsize = sformat_size_of(sFormat.format) * sFormat.channels;
829
830 AudioBufferList list;
831 AudioBuffer *buf = &list.mBuffers[0];
832
833 list.mNumberBuffers = 1;
834 buf->mNumberChannels = UInt32(sFormat.channels);
835 buf->mDataByteSize = UInt32(fsize * nframes);
836 buf->mData = const_cast<void *>(src);
837
838 UInt32 count = UInt32(nframes);
839 OSStatus os_res = ExtAudioFileWrite(eaf_ref(hHandle), count, &list);
840 if (os_res != kAudio_NoError)
841 return -set_error(decode_os_status(os_res));
842 return count;
843 #else
844 sf_count_t count;
845 status_t res;
846
847 switch (sformat_format(fmt))
848 {
849 case SFMT_S32:
850 count = sf_writef_int(hHandle, static_cast<const int *>(src), nframes);
851 break;
852
853 case SFMT_S16:
854 count = sf_writef_short(hHandle, static_cast<const short *>(src), nframes);
855 break;
856
857 case SFMT_F32:
858 count = sf_writef_float(hHandle, static_cast<const float *>(src), nframes);
859 break;
860
861 case SFMT_F64:
862 count = sf_writef_double(hHandle, static_cast<const double *>(src), nframes);
863 break;
864
865 default: // Force SFMT_F32 sample format
866 count = sf_writef_float(hHandle, static_cast<const float *>(src), nframes);
867 break;
868 }
869
870 // Is there data written?
871 if (count > 0)
872 return count;
873
874 res = decode_sf_error(hHandle);
875 return -((res == STATUS_OK) ? STATUS_EOF : res);
876 #endif
877 }
878
879 #if defined(PLATFORM_WINDOWS)
880 ssize_t OutAudioFileStream::conv_write(const void *src, size_t nframes, size_t fmt)
881 {
882 ssize_t res = IOutAudioStream::conv_write(src, nframes, fmt);
883 if (size_t(nOffset) > hHandle->nTotalFrames)
884 hHandle->nTotalFrames = nOffset;
885 return res;
886 }
887
888 ssize_t OutAudioFileStream::write_acm_convert(const void *src, size_t nframes)
889 {
890 size_t nwritten = 0;
891 void *dptr;
892 const uint8_t *sptr = static_cast<const uint8_t *>(src);
893 size_t fsize = sformat_size_of(sFormat.format) * sFormat.channels;
894 nframes *= fsize;
895
896 while (nwritten < nframes)
897 {
898 // Perform push if possible
899 ssize_t count = hHandle->pACM->push(&dptr);
900 if (count > 0) // We're still able to push data
901 {
902 // Copy data to buffer
903 ssize_t can_push = nframes - nwritten;
904 if (count > can_push)
905 count = can_push;
906 ::memcpy(dptr, sptr, count);
907
908 // Update pointers
909 sptr += count;
910 nwritten += count;
911 hHandle->pACM->commit(count);
912 continue;
913 }
914 else if (count < 0) // Error occurred?
915 {
916 if (nwritten > 0)
917 break;
918 return -set_error(STATUS_UNKNOWN_ERR);
919 }
920
921 // Need to perform flush
922 status_t res = flush_internal(false);
923 if (res != STATUS_OK)
924 {
925 if (nwritten > 0)
926 break;
927 return -set_error(res);
928 }
929 }
930
931 return nwritten / fsize;
932 }
933
934 #endif /* PLATFORM_WINDOWS */
935
936 size_t OutAudioFileStream::select_format(size_t rfmt)
937 {
938 #if defined(PLATFORM_WINDOWS)
939 if (hHandle->pFormat->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
940 return SFMT_F32_LE;
941
942 if (hHandle->pFormat->wFormatTag == WAVE_FORMAT_PCM)
943 {
944 switch (hHandle->pFormat->wBitsPerSample)
945 {
946 case 8: return SFMT_U8_CPU;
947 case 16: return SFMT_S16_LE;
948 case 24: return SFMT_S24_LE;
949 case 32: return SFMT_S32_LE;
950 default:
951 break;
952 }
953 }
954
955 return -1;
956 #elif defined(PLATFORM_MACOSX)
957 return sFormat.format;
958 #else
959 switch (sformat_format(rfmt))
960 {
961 #ifdef AFS_S32_CPU
962 case SFMT_S32:
963 case SFMT_U32:
964 case SFMT_S24:
965 case SFMT_U24:
966 return SFMT_S32_CPU;
967 #endif
968
969 #ifdef AFS_S16_CPU
970 case SFMT_S16:
971 case SFMT_U16:
972 case SFMT_S8:
973 case SFMT_U8:
974 return SFMT_S16_CPU;
975 #endif
976
977 case SFMT_F32:
978 return SFMT_F32_CPU;
979
980 case SFMT_F64:
981 return SFMT_F64_CPU;
982
983 default:
984 break;
985 }
986
987 return SFMT_F32_CPU;
988 #endif
989 }
990
991 wssize_t OutAudioFileStream::seek(wsize_t nframes)
992 {
993 if (is_closed())
994 return -set_error(STATUS_CLOSED);
995
996 #if defined(PLATFORM_WINDOWS)
997 size_t fsize = hHandle->pFormat->nBlockAlign;
998 if ((hHandle->pMMIO != NULL) && (bSeekable))
999 {
1000 wssize_t res = hHandle->pMMIO->seek(nframes * fsize);
1001 if (res < 0)
1002 {
1003 set_error(-res);
1004 return res;
1005 }
1006
1007 set_error(STATUS_OK);
1008 return res / fsize;
1009 }
1010
1011 return -set_error(STATUS_NOT_IMPLEMENTED);
1012 #elif defined(PLATFORM_MACOSX)
1013 OSStatus os_res = ExtAudioFileSeek(eaf_ref(hHandle), nframes);
1014 if (os_res != kAudio_NoError)
1015 return -set_error(decode_os_status(os_res));
1016
1017 nOffset = nframes;
1018 set_error(STATUS_OK);
1019 return nframes;
1020 #else
1021 sf_count_t offset = sf_seek(hHandle, nframes, SEEK_SET);
1022 if (offset < 0)
1023 return -set_error(decode_sf_error(hHandle));
1024
1025 set_error(STATUS_OK);
1026 return nOffset = offset;
1027 #endif /* USE_LIBSNDFILE */
1028 }
1029 } /* namespace mm */
1030 } /* namespace lsp */