Debian Codegraph @

modules/lsp-runtime-lib/src/main/mm/InAudioFileStream.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: 19 апр. 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/common/alloc.h>
23 #include <lsp-plug.in/common/debug.h>
24 #include <lsp-plug.in/common/endian.h>
25 #include <lsp-plug.in/io/File.h>
26 #include <lsp-plug.in/mm/InAudioFileStream.h>
27 #include <lsp-plug.in/stdlib/stdio.h>
28 #include <lsp-plug.in/stdlib/string.h>
29
30 #if defined(PLATFORM_WINDOWS)
31 #include <private/mm/ACMStream.h>
32 #include <private/mm/MMIOReader.h>
33
34 #include <windows.h>
35 #include <mmsystem.h>
36 #include <mmreg.h>
37 #include <msacm.h>
38 #endif /* PLATFORM_WINDOWS */
39
40 #if defined(PLATFORM_MACOSX)
41 #include <CoreFoundation/CFString.h>
42 #include <CoreFoundation/CFURL.h>
43 #include <AudioToolbox/ExtendedAudioFile.h>
44 #endif /* PLATFORM_MACOSX */
45
46 #ifdef USE_LIBSNDFILE
47 #if (__SIZEOF_INT__ == 4)
48 #define AFS_S32_CPU mm::SFMT_S32_CPU
49 #endif
50
51 #if (__SIZEOF_SHORT__ == 2)
52 #define AFS_S16_CPU mm::SFMT_S16_CPU
53 #elif ((__SIZEOF_SHORT__ == 4) && (!defined(AFS_S32_CPU)))
54 #define AFS_S32_CPU mm::SFMT_S32_CPU
55 #endif
56 #endif /* USE_LIBSNDFILE */
57
58 namespace lsp
59 {
60 namespace mm
61 {
62 #if defined(PLATFORM_WINDOWS)
63 struct WAVEFILE
64 {
65 MMIOReader *pMMIO;
66 ACMStream *pACM;
67 WAVEFORMATEX *pFormat;
68 };
69
70 static ssize_t decode_sample_format(WAVEFORMATEX *wfe)
71 {
72 if (wfe->wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
73 return SFMT_F32_LE;
74
75 if (wfe->wFormatTag == WAVE_FORMAT_PCM)
76 {
77 switch (wfe->wBitsPerSample)
78 {
79 case 8: return SFMT_U8_CPU;
80 case 16: return SFMT_S16_LE;
81 case 24: return SFMT_S24_LE;
82 case 32: return SFMT_S32_LE;
83 default:
84 break;
85 }
86 }
87
88 return -1;
89 }
90 #elif defined(PLATFORM_MACOSX)
91 static inline ExtAudioFileRef eaf_ref(void *handle)
92 {
93 return static_cast<ExtAudioFileRef>(handle);
94 }
95 #endif
96
97 InAudioFileStream::InAudioFileStream()
98 {
99 hHandle = NULL;
100 sFormat.srate = 0;
101 sFormat.channels= 0;
102 sFormat.frames = 0;
103 sFormat.format = mm::SFMT_NONE;
104 bSeekable = false;
105 }
106
107 InAudioFileStream::~InAudioFileStream()
108 {
109 IInAudioStream::close();
110 do_close();
111 }
112
113 status_t InAudioFileStream::do_close()
114 {
115 status_t res = close_handle(hHandle);
116
117 hHandle = NULL;
118 bSeekable = false;
119 nOffset = -1; // Mark as closed
120
121 // Cleanup format
122 sFormat.srate = 0;
123 sFormat.channels= 0;
124 sFormat.frames = 0;
125 sFormat.format = mm::SFMT_NONE;
126
127 return set_error(res);
128 }
129
130 status_t InAudioFileStream::close_handle(handle_t *h)
131 {
132 if (h == NULL)
133 return STATUS_OK;
134
135 #if defined(PLATFORM_WINDOWS)
136 status_t res = STATUS_OK;
137 if (h->pACM != NULL)
138 {
139 res = update_status(res, h->pACM->close());
140 delete h->pACM;
141 h->pACM = NULL;
142 }
143
144 if (h->pMMIO != NULL)
145 {
146 res = update_status(res, h->pMMIO->close());
147 delete h->pMMIO;
148 h->pMMIO = NULL;
149 }
150
151 h->pFormat = NULL; // Freed by owner
152 free(h);
153
154 return res;
155 #elif defined(PLATFORM_MACOSX)
156 OSStatus res = ExtAudioFileDispose(eaf_ref(h));
157 return decode_os_status(res);
158 #else
159 int res = sf_close(h);
160 return (res == 0) ? STATUS_OK : STATUS_IO_ERROR;
161 #endif
162 }
163
164 #if defined(PLATFORM_WINDOWS)
165 ssize_t InAudioFileStream::read_acm_convert(void *dst, size_t nframes, size_t fmt)
166 {
167 size_t nread = 0;
168 void *sptr;
169 uint8_t *dptr = static_cast<uint8_t *>(dst);
170 size_t fsize = sformat_size_of(sFormat.format) * sFormat.channels;
171 nframes *= fsize;
172 bool eof = false;
173
174 while (nread < nframes)
175 {
176 // Perform pull
177 size_t can_pull = nframes - nread;
178 ssize_t count = hHandle->pACM->pull(&sptr, can_pull, eof);
179 if (count > 0)
180 {
181 // Copy data to dptr
182 ::memcpy(dptr, sptr, count);
183
184 // Update pointers and repeat pull
185 dptr += count;
186 nread += count;
187 continue;
188 }
189 else if (count < 0)
190 {
191 if (nread > 0)
192 break;// Analyze result
193 set_error(-count);
194 return count;
195 }
196
197 // Perform push if possible
198 count = hHandle->pACM->push(&sptr);
199 if (count < 0)
200 {
201 if (nread > 0)
202 break;
203 return -set_error(STATUS_UNKNOWN_ERR);
204 }
205
206 // Read data into buffer block and commit new position
207 count = hHandle->pMMIO->read(sptr, count);
208 if (count < 0)
209 {
210 if (count == -STATUS_EOF)
211 eof = true;
212 else if (nread > 0)
213 break;
214 set_error(-count);
215 return count;
216 }
217
218 hHandle->pACM->commit(count);
219 }
220
221 return nread / fsize;
222 }
223 #elif defined(PLATFORM_MACOSX)
224 status_t InAudioFileStream::decode_os_status(uint32_t code)
225 {
226 return (code == kAudio_NoError) ? STATUS_OK : STATUS_UNKNOWN_ERR;
227 }
228 #else
229 status_t InAudioFileStream::decode_sf_error(SNDFILE *fd)
230 {
231 const int error = sf_error(fd);
232 switch (error)
233 {
234 case SF_ERR_NO_ERROR:
235 return STATUS_OK;
236 case SF_ERR_UNRECOGNISED_FORMAT:
237 return STATUS_UNSUPPORTED_FORMAT;
238 case SF_ERR_MALFORMED_FILE:
239 return STATUS_CORRUPTED_FILE;
240 case SF_ERR_UNSUPPORTED_ENCODING:
241 return STATUS_UNSUPPORTED_FORMAT;
242 default:
243 return STATUS_UNKNOWN_ERR;
244 }
245 }
246 #endif
247
248 status_t InAudioFileStream::open(const char *path)
249 {
250 if (path == NULL)
251 return set_error(STATUS_BAD_ARGUMENTS);
252 LSPString xpath;
253 if (!xpath.set_utf8(path))
254 return set_error(STATUS_NO_MEM);
255 return open(&xpath);
256 }
257
258 status_t InAudioFileStream::open(const io::Path *path)
259 {
260 if (path == NULL)
261 return set_error(STATUS_BAD_ARGUMENTS);
262 return open(path->as_string());
263 }
264
265 status_t InAudioFileStream::open(const LSPString *path)
266 {
267 if (!is_closed())
268 return -set_error(STATUS_OPENED);
269
270 #if defined(PLATFORM_WINDOWS)
271 status_t res = STATUS_OK;
272
273 // Allocate handle
274 handle_t *h = static_cast<handle_t *>(malloc(sizeof(handle_t)));
275 if (h == NULL)
276 return set_error(STATUS_NO_MEM);
277 h->pMMIO = NULL;
278 h->pACM = NULL;
279 h->pFormat = NULL;
280 lsp_finally { close_handle(h); };
281
282 // Try to load data using MMIO
283 h->pMMIO = new MMIOReader();
284 if (h->pMMIO == NULL)
285 return -set_error(STATUS_NO_MEM);
286 if ((res = h->pMMIO->open(path)) != STATUS_OK)
287 return set_error(res);
288
289 // Analyze format
290 ssize_t fmt;
291 WAVEFORMATEX *wfe = h->pMMIO->format();
292 if ((wfe->wFormatTag == WAVE_FORMAT_PCM) || (wfe->wFormatTag == WAVE_FORMAT_IEEE_FLOAT))
293 {
294 if ((fmt = decode_sample_format(wfe)) <= 0)
295 return set_error(STATUS_UNSUPPORTED_FORMAT);
296
297 // All is OK, use reading of PCM samples
298 h->pACM = NULL;
299 h->pFormat = wfe;
300
301 sFormat.srate = wfe->nSamplesPerSec;
302 sFormat.channels = wfe->nChannels;
303 sFormat.frames = h->pMMIO->frames();
304 sFormat.format = fmt;
305
306 nOffset = 0;
307 bSeekable = h->pMMIO->seekable();
308
309 // Commit handle and return
310 lsp::swap(hHandle, h);
311 return set_error(STATUS_OK);
312 }
313
314 // Create ACM stream
315 h->pACM = new ACMStream();
316 if (h->pACM == NULL)
317 return set_error(STATUS_NO_MEM);
318 if ((res = h->pACM->read_pcm(wfe)) != STATUS_OK)
319 return set_error(res);
320
321 // Detect format
322 wfe = h->pACM->out_format();
323 if ((fmt = decode_sample_format(wfe)) <= 0)
324 return set_error(STATUS_UNSUPPORTED_FORMAT);
325
326 // All is ok
327 h->pFormat = h->pACM->out_format();
328
329 sFormat.srate = wfe->nSamplesPerSec;
330 sFormat.channels = wfe->nChannels;
331 sFormat.frames = h->pMMIO->frames();
332 sFormat.format = fmt;
333
334 nOffset = 0;
335 bSeekable = false;
336
337 // Commit handle and return
338 lsp::swap(hHandle, h);
339 return set_error(STATUS_OK);
340 #elif defined(PLATFORM_MACOSX)
341 // Form the URL to open
342 CFStringRef str_ref = CFStringCreateWithCString(
343 kCFAllocatorDefault,
344 path->get_utf8(),
345 kCFStringEncodingUTF8);
346 lsp_finally { CFRelease(str_ref); };
347 CFURLRef url_ref = CFURLCreateWithFileSystemPath(
348 kCFAllocatorDefault,
349 str_ref,
350 kCFURLPOSIXPathStyle,
351 false);
352 lsp_finally { CFRelease(url_ref); };
353
354 // Open file
355 ExtAudioFileRef eaf = NULL;
356 OSStatus os_res = ExtAudioFileOpenURL(url_ref, &eaf);
357 if (os_res != kAudio_NoError)
358 return set_error(decode_os_status(os_res));
359 lsp_finally {
360 if (eaf != NULL)
361 ExtAudioFileDispose(eaf);
362 };
363
364 // Read audio format
365 AudioStreamBasicDescription info;
366 UInt32 szof_info = sizeof(AudioStreamBasicDescription);
367 bzero(&info, szof_info);
368 os_res = ExtAudioFileGetProperty(
369 eaf,
370 kExtAudioFileProperty_FileDataFormat,
371 &szof_info,
372 &info);
373 if (os_res != kAudio_NoError)
374 return set_error(decode_os_status(os_res));
375
376 // Read audio file length if present
377 SInt64 num_frames = -1;
378 os_res = ExtAudioFileGetProperty(
379 eaf,
380 kExtAudioFileProperty_FileLengthFrames,
381 &szof_info,
382 &num_frames);
383 if (os_res != kAudio_NoError)
384 num_frames = -1;
385
386 // Decode sample format
387 size_t format = 0;
388 size_t be_flag = (info.mFormatFlags & kAudioFormatFlagIsBigEndian) ? mm::SFMT_BE : mm::SFMT_LE;
389 bool need_convert = info.mFormatID != kAudioFormatLinearPCM;
390 if (!need_convert)
391 {
392 if (info.mFormatFlags & kAudioFormatFlagIsFloat)
393 {
394 if (info.mBitsPerChannel == sizeof(f32_t) * 8)
395 format = mm::SFMT_F32 | be_flag;
396 else if (info.mBitsPerChannel == sizeof(f64_t) * 8)
397 format = mm::SFMT_F64 | be_flag;
398 else
399 {
400 format = mm::SFMT_F32_CPU;
401 need_convert = true;
402 }
403 }
404 else if (info.mFormatFlags & kAudioFormatFlagIsSignedInteger)
405 {
406 if (info.mBitsPerChannel == sizeof(int8_t) * 8)
407 format = mm::SFMT_S8 | be_flag;
408 else if (info.mBitsPerChannel == sizeof(int16_t) * 8)
409 format = mm::SFMT_S16 | be_flag;
410 else if (info.mBitsPerChannel == sizeof(int8_t) * 24)
411 format = mm::SFMT_S24 | be_flag;
412 else if (info.mBitsPerChannel == sizeof(int32_t) * 8)
413 format = mm::SFMT_S32 | be_flag;
414 else
415 need_convert = true;
416 }
417 else
418 {
419 if (info.mBitsPerChannel == sizeof(uint8_t) * 8)
420 format = mm::SFMT_U8 | be_flag;
421 else if (info.mBitsPerChannel == sizeof(uint16_t) * 8)
422 format = mm::SFMT_U16 | be_flag;
423 else if (info.mBitsPerChannel == sizeof(uint8_t) * 24)
424 format = mm::SFMT_U24 | be_flag;
425 else if (info.mBitsPerChannel == sizeof(uint32_t) * 8)
426 format = mm::SFMT_U32 | be_flag;
427 else
428 need_convert = true;
429 }
430 }
431
432 // Initialize audio converter if needed to match the selected sample format
433 if (need_convert)
434 {
435 AudioStreamBasicDescription cvt;
436 bzero(&cvt, sizeof(AudioStreamBasicDescription));
437 cvt.mSampleRate = info.mSampleRate;
438 cvt.mFormatID = kAudioFormatLinearPCM;
439 cvt.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
440
441 if (info.mBitsPerChannel <= sizeof(f32_t)*8)
442 {
443 cvt.mBytesPerPacket = sizeof(f32_t) * info.mChannelsPerFrame;
444 cvt.mFramesPerPacket = 1;
445 cvt.mBytesPerFrame = sizeof(f32_t) * info.mChannelsPerFrame;
446 cvt.mChannelsPerFrame = info.mChannelsPerFrame;
447 cvt.mBitsPerChannel = sizeof(f32_t) * 8;
448 format = mm::SFMT_F32 | be_flag;
449 }
450 else
451 {
452 cvt.mBytesPerPacket = sizeof(f64_t) * info.mChannelsPerFrame;
453 cvt.mFramesPerPacket = 1;
454 cvt.mBytesPerFrame = sizeof(f64_t) * info.mChannelsPerFrame;
455 cvt.mChannelsPerFrame = info.mChannelsPerFrame;
456 cvt.mBitsPerChannel = sizeof(f64_t) * 8;
457 format = mm::SFMT_F64 | be_flag;
458 }
459
460 os_res = ExtAudioFileSetProperty(
461 eaf,
462 kExtAudioFileProperty_ClientDataFormat,
463 sizeof(AudioStreamBasicDescription),
464 &cvt);
465
466 if (os_res != kAudio_NoError)
467 return set_error(decode_os_status(os_res));
468 }
469
470 // Update state and return
471 sFormat.srate = info.mSampleRate;
472 sFormat.channels = info.mChannelsPerFrame;
473 sFormat.frames = num_frames;
474 sFormat.format = format;
475
476 nOffset = 0;
477 bSeekable = false;
478 hHandle = release_ptr(eaf);
479
480 return set_error(STATUS_OK);
481 #else
482 SF_INFO info;
483 SNDFILE *sf;
484
485 // Open file for reading
486 // Note! From documentation:
487 // When opening a file for read, the format field should be set to zero before calling sf_open().
488 // The only exception to this is the case of RAW files where the caller has to set the samplerate, channels
489 // and format fields to valid values. All other fields of the structure are filled in by the library.
490 info.format = 0;
491 if ((sf = sf_open(path->get_native(), SFM_READ, &info)) == NULL)
492 {
493 status_t res = decode_sf_error(sf);
494 if (res == STATUS_UNKNOWN_ERR)
495 {
496 // Need more information about the file
497 io::fattr_t fattr;
498 status_t ires = res;
499 if ((ires = io::File::stat(path, &fattr)) == STATUS_OK)
500 {
501 if (fattr.type == io::fattr_t::FT_SYMLINK)
502 res = STATUS_BAD_SYMLINK;
503 else if (fattr.type != io::fattr_t::FT_REGULAR)
504 res = STATUS_NOT_FILE;
505 }
506 else if ((ires = io::File::sym_stat(path, &fattr)) == STATUS_OK)
507 {
508 if (fattr.type != io::fattr_t::FT_SYMLINK)
509 res = STATUS_BAD_SYMLINK;
510 }
511 else
512 res = ires;
513 }
514
515 return set_error(res);
516 }
517
518 // Decode metadata
519 sFormat.srate = info.samplerate;
520 sFormat.channels = info.channels;
521 sFormat.frames = info.frames;
522
523 // Decode sample format
524 switch (info.format & SF_FORMAT_SUBMASK)
525 {
526 case SF_FORMAT_PCM_U8: sFormat.format = mm::SFMT_U8_CPU; break;
527 case SF_FORMAT_PCM_S8: sFormat.format = mm::SFMT_S8_CPU; break;
528 case SF_FORMAT_PCM_16: sFormat.format = mm::SFMT_S16_CPU; break;
529 case SF_FORMAT_PCM_24: sFormat.format = mm::SFMT_S24_CPU; break;
530 case SF_FORMAT_PCM_32: sFormat.format = mm::SFMT_S32_CPU; break;
531 case SF_FORMAT_FLOAT: sFormat.format = mm::SFMT_F32_CPU; break;
532 case SF_FORMAT_DOUBLE: sFormat.format = mm::SFMT_F64_CPU; break;
533 default: sFormat.format = mm::SFMT_F32_CPU; break;
534 }
535
536 // Commit new state
537 hHandle = sf;
538 nOffset = 0;
539 bSeekable = info.seekable;
540
541 return set_error(STATUS_OK);
542 #endif
543 }
544
545 status_t InAudioFileStream::close()
546 {
547 IInAudioStream::close();
548 return do_close();
549 }
550
551 size_t InAudioFileStream::select_format(size_t fmt)
552 {
553 #ifdef USE_LIBSNDFILE
554 // libsndfile allows one to do some sample conversions internally
555 // we trust it more than our own sample converison routines
556 switch (sformat_format(fmt))
557 {
558 #ifdef AFS_S32_CPU
559 case SFMT_S32:
560 case SFMT_U32:
561 case SFMT_S24:
562 case SFMT_U24:
563 return SFMT_S32_CPU;
564 #endif
565
566 #ifdef AFS_S16_CPU
567 case SFMT_S16:
568 case SFMT_U16:
569 case SFMT_S8:
570 case SFMT_U8:
571 return SFMT_S16_CPU;
572 #endif
573
574 case SFMT_F32:
575 return SFMT_F32_CPU;
576
577 case SFMT_F64:
578 return SFMT_F64_CPU;
579
580 default:
581 break;
582 }
583
584 return SFMT_F32_CPU;
585 #else
586 // We always do sample conversion for PCM stream
587 return sFormat.format;
588 #endif
589 }
590
591 ssize_t InAudioFileStream::direct_read(void *dst, size_t nframes, size_t fmt)
592 {
593 #if defined(PLATFORM_WINDOWS)
594 if (hHandle->pMMIO == NULL)
595 return -STATUS_NOT_SUPPORTED;
596 if (hHandle->pACM != NULL)
597 return read_acm_convert(dst, nframes, fmt);
598
599 size_t fsize = sformat_size_of(sFormat.format) * sFormat.channels;
600 ssize_t nread = hHandle->pMMIO->read(dst, fsize * nframes);
601 return (nread < 0) ? nread : nread / fsize;
602 #elif defined(PLATFORM_MACOSX)
603 const size_t fsize = sformat_size_of(sFormat.format) * sFormat.channels;
604
605 AudioBufferList list;
606 AudioBuffer *buf = &list.mBuffers[0];
607
608 list.mNumberBuffers = 1;
609 buf->mNumberChannels = UInt32(sFormat.channels);
610 buf->mDataByteSize = UInt32(fsize * nframes);
611 buf->mData = dst;
612
613 UInt32 count = UInt32(nframes);
614 OSStatus os_res = ExtAudioFileRead(eaf_ref(hHandle), &count, &list);
615 if (os_res != kAudio_NoError)
616 return -set_error(decode_os_status(os_res));
617 if (count > 0)
618 return count;
619
620 return -STATUS_EOF;
621 #else
622 sf_count_t count;
623 status_t res;
624
625 switch (sformat_format(fmt))
626 {
627 case SFMT_S32:
628 count = sf_readf_int(hHandle, static_cast<int *>(dst), nframes);
629 break;
630 case SFMT_S16:
631 count = sf_readf_short(hHandle, static_cast<short *>(dst), nframes);
632 break;
633 case SFMT_F32:
634 count = sf_readf_float(hHandle, static_cast<float *>(dst), nframes);
635 break;
636 case SFMT_F64:
637 count = sf_readf_double(hHandle, static_cast<double *>(dst), nframes);
638 break;
639 default: // Force SFMT_F32 sample format
640 count = sf_readf_float(hHandle, static_cast<float *>(dst), nframes);
641 break;
642 }
643
644 // Is there data written?
645 if (count > 0)
646 return count;
647
648 res = decode_sf_error(hHandle);
649 return -((res == STATUS_OK) ? STATUS_EOF : res);
650 #endif
651 }
652
653 wssize_t InAudioFileStream::skip(wsize_t nframes)
654 {
655 if (is_closed())
656 return -set_error(STATUS_CLOSED);
657 if (!bSeekable)
658 return IInAudioStream::skip(nframes);
659
660 #if defined(PLATFORM_WINDOWS)
661 if (hHandle->pMMIO != NULL)
662 {
663 size_t fsize = sformat_size_of(sFormat.format) * LE_TO_CPU(hHandle->pFormat->nChannels);
664 wssize_t res = hHandle->pMMIO->seek((nOffset + nframes) * fsize);
665 if (res < 0)
666 {
667 set_error(-res);
668 return res;
669 }
670 nOffset = res / fsize;
671 return set_error(STATUS_OK);
672 }
673
674 return -set_error(STATUS_NOT_SUPPORTED);
675 #elif defined(PLATFORM_MACOSX)
676 OSStatus os_res = ExtAudioFileSeek(eaf_ref(hHandle), nOffset + nframes);
677 if (os_res != kAudio_NoError)
678 return -set_error(decode_os_status(os_res));
679
680 nOffset += nframes;
681 set_error(STATUS_OK);
682 return nframes;
683 #else
684 sf_count_t res = sf_seek(hHandle, nframes, SEEK_CUR);
685 if (res >= 0)
686 {
687 nOffset += nframes;
688 set_error(STATUS_OK);
689 return nframes;
690 }
691
692 return -set_error(decode_sf_error(hHandle));
693 #endif
694 }
695
696 wssize_t InAudioFileStream::seek(wsize_t nframes)
697 {
698 if (is_closed())
699 return -set_error(STATUS_CLOSED);
700 if (!bSeekable)
701 return IInAudioStream::seek(nframes);
702
703 #if defined(PLATFORM_WINDOWS)
704 if (hHandle->pMMIO != NULL)
705 {
706 size_t fsize = sformat_size_of(sFormat.format) * LE_TO_CPU(hHandle->pFormat->nChannels);
707 wssize_t res = hHandle->pMMIO->seek(nframes * fsize);
708 if (res < 0)
709 {
710 set_error(-res);
711 return res;
712 }
713 nOffset = res / fsize;
714 return set_error(STATUS_OK);
715 }
716
717 return -set_error(STATUS_NOT_SUPPORTED);
718 #elif defined(PLATFORM_MACOSX)
719 OSStatus os_res = ExtAudioFileSeek(eaf_ref(hHandle), nframes);
720 if (os_res != kAudio_NoError)
721 return -set_error(decode_os_status(os_res));
722
723 nOffset = nframes;
724 set_error(STATUS_OK);
725 return nframes;
726 #else
727 sf_count_t res = sf_seek(hHandle, nframes, SEEK_SET);
728 if (res >= 0)
729 {
730 nOffset = nframes;
731 set_error(STATUS_OK);
732 return nframes;
733 }
734
735 return -set_error(decode_sf_error(hHandle));
736 #endif
737 }
738
739 status_t InAudioFileStream::info(mm::audio_stream_t *dst) const
740 {
741 if (dst == NULL)
742 return STATUS_BAD_ARGUMENTS;
743 *dst = sFormat;
744 return STATUS_OK;
745 }
746
747 size_t InAudioFileStream::sample_rate() const
748 {
749 return sFormat.srate;
750 }
751
752 size_t InAudioFileStream::channels() const
753 {
754 return sFormat.channels;
755 }
756
757 wssize_t InAudioFileStream::length() const
758 {
759 return sFormat.frames;
760 }
761
762 size_t InAudioFileStream::format() const
763 {
764 return sFormat.format;
765 }
766
767 } /* namespace mm */
768 } /* namespace lsp */