2 * Copyright © 2000-2006 Ingo Weinhold <ingo_weinhold@gmx.de>
3 * All rights reserved. Distributed under the terms of the MIT licensce.
8 #include "AudioReader.h"
13 AudioReader::AudioReader()
20 AudioReader::AudioReader(const media_format
& format
)
27 AudioReader::~AudioReader()
33 AudioReader::InitCheck() const
40 AudioReader::SetFormat(const media_format
& format
)
47 AudioReader::Format() const
54 AudioReader::SetOutOffset(int64 offset
)
61 AudioReader::OutOffset() const
68 AudioReader::FrameForTime(bigtime_t time
) const
70 double frameRate
= fFormat
.u
.raw_audio
.frame_rate
;
71 return int64(double(time
) * frameRate
/ 1000000.0);
76 AudioReader::TimeForFrame(int64 frame
) const
78 double frameRate
= fFormat
.u
.raw_audio
.frame_rate
;
79 return bigtime_t(double(frame
) * 1000000.0 / frameRate
);
83 //! helper function for ReadSilence()
84 template<typename sample_t
>
86 fill_buffer(void* buffer
, int32 count
, sample_t value
)
88 sample_t
* buf
= (sample_t
*)buffer
;
89 sample_t
* bufferEnd
= buf
+ count
;
90 for (; buf
< bufferEnd
; buf
++)
95 /*! Fills the supplied buffer with /frames/ frames of silence and returns a
96 pointer to the frames after the filled range.
97 /frames/ must be >= 0.*/
99 AudioReader::ReadSilence(void* buffer
, int64 frames
) const
101 void* bufferEnd
= SkipFrames(buffer
, frames
);
102 int32 sampleCount
= frames
* fFormat
.u
.raw_audio
.channel_count
;
103 switch (fFormat
.u
.raw_audio
.format
) {
104 case media_raw_audio_format::B_AUDIO_FLOAT
:
105 fill_buffer(buffer
, sampleCount
, (float)0);
107 case media_raw_audio_format::B_AUDIO_INT
:
108 fill_buffer(buffer
, sampleCount
, (int)0);
110 case media_raw_audio_format::B_AUDIO_SHORT
:
111 fill_buffer(buffer
, sampleCount
, (short)0);
113 case media_raw_audio_format::B_AUDIO_UCHAR
:
114 fill_buffer(buffer
, sampleCount
, (uchar
)128);
116 case media_raw_audio_format::B_AUDIO_CHAR
:
117 fill_buffer(buffer
, sampleCount
, (uchar
)0);
120 memset(buffer
, 0, (char*)bufferEnd
- (char*)buffer
);
127 //! Returns a buffer pointer offset by /frames/ frames.
129 AudioReader::SkipFrames(void* buffer
, int64 frames
) const
131 int32 sampleSize
= fFormat
.u
.raw_audio
.format
132 & media_raw_audio_format::B_AUDIO_SIZE_MASK
;
133 int32 frameSize
= sampleSize
* fFormat
.u
.raw_audio
.channel_count
;
134 return (char*)buffer
+ frames
* frameSize
;
139 AudioReader::ReverseFrames(void* buffer
, int64 frames
) const
141 int32 sampleSize
= fFormat
.u
.raw_audio
.format
142 & media_raw_audio_format::B_AUDIO_SIZE_MASK
;
143 int32 frameSize
= sampleSize
* fFormat
.u
.raw_audio
.channel_count
;
144 char* front
= (char*)buffer
;
145 char* back
= (char*)buffer
+ (frames
- 1) * frameSize
;
146 while (front
< back
) {
147 for (int32 i
= 0; i
< frameSize
; i
++)
148 swap(front
[i
], back
[i
]);