1 /***********************************************************************
2 * AUTHOR: Marcus Overhagen
5 ***********************************************************************/
7 #include <MediaTrack.h>
14 /*************************************************************
16 *************************************************************/
18 BSoundFile::BSoundFile()
24 BSoundFile::BSoundFile(const entry_ref
*ref
,
32 BSoundFile::~BSoundFile()
36 // fMediaTrack will be deleted by the BMediaFile destructor
41 BSoundFile::InitCheck() const
46 return fSoundFile
->InitCheck();
51 BSoundFile::SetTo(const entry_ref
*ref
,
55 BMediaTrack
* track
= fMediaTrack
;
57 fMediaFile
->ReleaseTrack(track
);
60 BMediaFile
* file
= fMediaFile
;
65 BFile
* file
= fSoundFile
;
69 if (open_mode
== B_READ_ONLY
) {
70 return _ref_to_file(ref
);
79 BSoundFile::FileFormat() const
86 BSoundFile::SamplingRate() const
93 BSoundFile::CountChannels() const
100 BSoundFile::SampleSize() const
107 BSoundFile::ByteOrder() const
114 BSoundFile::SampleFormat() const
116 return fSampleFormat
;
121 BSoundFile::FrameSize() const
123 return fSampleSize
* fChannelCount
;
128 BSoundFile::CountFrames() const
135 BSoundFile::IsCompressed() const
137 return fIsCompressed
;
142 BSoundFile::CompressionType() const
144 return fCompressionType
;
149 BSoundFile::CompressionName() const
151 return fCompressionName
;
156 BSoundFile::SetFileFormat(int32 format
)
158 fFileFormat
= format
;
164 BSoundFile::SetSamplingRate(int32 fps
)
167 return fSamplingRate
;
172 BSoundFile::SetChannelCount(int32 spf
)
175 return fChannelCount
;
180 BSoundFile::SetSampleSize(int32 bps
)
188 BSoundFile::SetByteOrder(int32 bord
)
196 BSoundFile::SetSampleFormat(int32 fmt
)
199 return fSampleFormat
;
204 BSoundFile::SetCompressionType(int32 type
)
211 BSoundFile::SetCompressionName(char *name
)
218 BSoundFile::SetIsCompressed(bool tf
)
225 BSoundFile::SetDataLocation(off_t offset
)
234 BSoundFile::SetFrameCount(off_t count
)
242 BSoundFile::ReadFrames(char *buf
,
245 size_t frameRead
= 0;
246 int64 frames
= count
;
248 status_t status
= fMediaTrack
->ReadFrames(
249 reinterpret_cast<void *>(buf
), &frames
);
252 buf
+= fSampleSize
* fChannelCount
* frames
;
253 if (status
!= B_OK
) {
264 BSoundFile::WriteFrames(char *buf
,
267 return fMediaTrack
->WriteFrames(
268 reinterpret_cast<void *>(buf
), count
);
273 BSoundFile::SeekToFrame(off_t n
)
276 status_t status
= fMediaTrack
->SeekToFrame(&frames
);
286 BSoundFile::FrameIndex() const
293 BSoundFile::FramesRemaining() const
295 return fFrameCount
- FrameIndex();
298 /*************************************************************
300 *************************************************************/
303 void BSoundFile::_ReservedSoundFile1() {}
304 void BSoundFile::_ReservedSoundFile2() {}
305 void BSoundFile::_ReservedSoundFile3() {}
308 BSoundFile::_init_raw_stats()
313 fFileFormat
= B_UNKNOWN_FILE
;
314 fSamplingRate
= 44100;
317 fByteOrder
= B_BIG_ENDIAN
;
318 fSampleFormat
= B_LINEAR_SAMPLES
;
321 fIsCompressed
= false;
322 fCompressionType
= -1;
323 fCompressionName
= NULL
;
328 _ParseMimeType(char *mime_type
)
330 if (strcmp(mime_type
, "audio/x-aiff") == 0)
332 if (strcmp(mime_type
, "audio/x-wav") == 0)
334 return B_UNKNOWN_FILE
;
339 BSoundFile::_ref_to_file(const entry_ref
*ref
)
342 BFile
* file
= new BFile(ref
, B_READ_ONLY
);
343 status
= file
->InitCheck();
344 if (status
!= B_OK
) {
348 BMediaFile
* media
= new BMediaFile(file
);
349 status
= media
->InitCheck();
350 if (status
!= B_OK
) {
355 media_file_format mfi
;
356 media
->GetFileFormatInfo(&mfi
);
357 switch (mfi
.family
) {
358 case B_AIFF_FORMAT_FAMILY
: fFileFormat
= B_AIFF_FILE
; break;
359 case B_WAV_FORMAT_FAMILY
: fFileFormat
= B_WAVE_FILE
; break;
360 default: fFileFormat
= _ParseMimeType(mfi
.mime_type
); break;
363 BMediaTrack
* track
= 0;
365 while (trackNum
< media
->CountTracks()) {
366 track
= media
->TrackAt(trackNum
);
367 status
= track
->DecodedFormat(&mf
);
368 if (status
!= B_OK
) {
369 media
->ReleaseTrack(track
);
377 media
->ReleaseTrack(track
);
385 media_raw_audio_format
* raw
= 0;
386 if (mf
.type
== B_MEDIA_ENCODED_AUDIO
) {
387 raw
= &mf
.u
.encoded_audio
.output
;
389 if (mf
.type
== B_MEDIA_RAW_AUDIO
) {
390 raw
= &mf
.u
.raw_audio
;
399 fSamplingRate
= (int)raw
->frame_rate
;
400 fChannelCount
= raw
->channel_count
;
401 fSampleSize
= raw
->format
& 0xf;
402 fByteOrder
= raw
->byte_order
;
403 switch (raw
->format
) {
404 case media_raw_audio_format::B_AUDIO_FLOAT
:
405 fSampleFormat
= B_FLOAT_SAMPLES
;
407 case media_raw_audio_format::B_AUDIO_INT
:
408 case media_raw_audio_format::B_AUDIO_SHORT
:
409 case media_raw_audio_format::B_AUDIO_UCHAR
:
410 case media_raw_audio_format::B_AUDIO_CHAR
:
411 fSampleFormat
= B_LINEAR_SAMPLES
;
414 fSampleFormat
= B_UNDEFINED_SAMPLES
;
417 fFrameCount
= track
->CountFrames();
419 if (mf
.type
== B_MEDIA_ENCODED_AUDIO
) {
420 fIsCompressed
= true;
421 fCompressionType
= mf
.u
.encoded_audio
.encoding
;