btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / mediaplayer / media_node_framework / audio / AudioReader.cpp
blobe770b13764fe62ffbd051e44d216619261cb903c
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 <algorithm>
6 #include <string.h>
8 #include "AudioReader.h"
10 using std::swap;
13 AudioReader::AudioReader()
14 : fFormat(),
15 fOutOffset(0)
20 AudioReader::AudioReader(const media_format& format)
21 : fFormat(format),
22 fOutOffset(0)
27 AudioReader::~AudioReader()
32 status_t
33 AudioReader::InitCheck() const
35 return B_OK;
39 void
40 AudioReader::SetFormat(const media_format& format)
42 fFormat = format;
46 const media_format&
47 AudioReader::Format() const
49 return fFormat;
53 void
54 AudioReader::SetOutOffset(int64 offset)
56 fOutOffset = offset;
60 int64
61 AudioReader::OutOffset() const
63 return fOutOffset;
67 int64
68 AudioReader::FrameForTime(bigtime_t time) const
70 double frameRate = fFormat.u.raw_audio.frame_rate;
71 return int64(double(time) * frameRate / 1000000.0);
75 bigtime_t
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>
85 inline void
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++)
91 *buf = value;
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.*/
98 void*
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);
106 break;
107 case media_raw_audio_format::B_AUDIO_INT:
108 fill_buffer(buffer, sampleCount, (int)0);
109 break;
110 case media_raw_audio_format::B_AUDIO_SHORT:
111 fill_buffer(buffer, sampleCount, (short)0);
112 break;
113 case media_raw_audio_format::B_AUDIO_UCHAR:
114 fill_buffer(buffer, sampleCount, (uchar)128);
115 break;
116 case media_raw_audio_format::B_AUDIO_CHAR:
117 fill_buffer(buffer, sampleCount, (uchar)0);
118 break;
119 default:
120 memset(buffer, 0, (char*)bufferEnd - (char*)buffer);
121 break;
123 return bufferEnd;
127 //! Returns a buffer pointer offset by /frames/ frames.
128 void*
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;
138 void
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]);
149 front += frameSize;
150 back -= frameSize;