1 /* 2 * Copyright © 2000-2006 Ingo Weinhold <ingo_weinhold@gmx.de> 3 * All rights reserved. Distributed under the terms of the MIT licensce. 4 */ 5 #include "AudioAdapter.h" 6 7 #include <new> 8 #include <algobase.h> 9 #include <stdio.h> 10 #include <string.h> 11 12 #include <ByteOrder.h> 13 14 #include "AudioChannelConverter.h" 15 #include "AudioFormatConverter.h" 16 #include "AudioResampler.h" 17 18 using std::nothrow; 19 20 #define TRACE_AUDIO_ADAPTER 21 #ifdef TRACE_AUDIO_ADAPTER 22 # define TRACE(x...) printf(x) 23 #else 24 # define TRACE(x...) 25 #endif 26 27 28 AudioAdapter::AudioAdapter(AudioReader* source, const media_format& format) 29 : AudioReader(format), 30 fSource(source), 31 fFinalConverter(NULL), 32 fFormatConverter(NULL), 33 fChannelConverter(NULL), 34 fResampler(NULL) 35 { 36 uint32 hostByteOrder 37 = (B_HOST_IS_BENDIAN) ? B_MEDIA_BIG_ENDIAN : B_MEDIA_LITTLE_ENDIAN; 38 fFormat.u.raw_audio.byte_order = hostByteOrder; 39 if (source && source->Format().type == B_MEDIA_RAW_AUDIO) { 40 41 if (fFormat.u.raw_audio.format != source->Format().u.raw_audio.format 42 || source->Format().u.raw_audio.byte_order != hostByteOrder) { 43 TRACE("AudioAdapter() - using format converter\n"); 44 fFormatConverter = new (nothrow) AudioFormatConverter(source, 45 fFormat.u.raw_audio.format, hostByteOrder); 46 source = fFormatConverter; 47 } 48 49 if (fFormat.u.raw_audio.frame_rate 50 != source->Format().u.raw_audio.frame_rate) { 51 TRACE("AudioAdapter() - using resampler\n"); 52 fResampler = new (nothrow) AudioResampler(source, 53 fFormat.u.raw_audio.frame_rate); 54 source = fResampler; 55 } 56 57 if (fFormat.u.raw_audio.channel_count 58 != source->Format().u.raw_audio.channel_count) { 59 TRACE("AudioAdapter() - using channel converter\n"); 60 fChannelConverter = new (nothrow) AudioChannelConverter(source, 61 fFormat); 62 source = fChannelConverter; 63 } 64 65 fFinalConverter = source; 66 } else 67 fSource = NULL; 68 } 69 70 71 AudioAdapter::~AudioAdapter() 72 { 73 delete fFormatConverter; 74 delete fChannelConverter; 75 delete fResampler; 76 } 77 78 79 status_t 80 AudioAdapter::Read(void* buffer, int64 pos, int64 frames) 81 { 82 // TRACE("AudioAdapter::Read(%p, %Ld, %Ld)\n", buffer, pos, frames); 83 status_t error = InitCheck(); 84 if (error != B_OK) 85 return error; 86 pos += fOutOffset; 87 status_t ret = fFinalConverter->Read(buffer, pos, frames); 88 // TRACE("AudioAdapter::Read() done: %s\n", strerror(ret)); 89 return ret; 90 } 91 92 93 status_t 94 AudioAdapter::InitCheck() const 95 { 96 status_t error = AudioReader::InitCheck(); 97 if (error == B_OK && !fFinalConverter) 98 error = B_NO_INIT; 99 if (error == B_OK && fFinalConverter) 100 error = fFinalConverter->InitCheck(); 101 return error; 102 } 103 104 105 AudioReader* 106 AudioAdapter::Source() const 107 { 108 return fSource; 109 } 110 111