2 * Copyright 2009, Haiku Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
7 * Michael Lotz <mmlr@mlotz.ch>
13 #include "TrackReader.h"
20 BSound::BSound(void* data
, size_t size
, const media_raw_audio_format
& format
,
28 fFreeWhenDone(freeWhenDone
),
38 BSound::BSound(const entry_ref
* soundFile
, bool loadIntoMemory
)
41 fFile(new(std::nothrow
) BFile(soundFile
, B_READ_ONLY
)),
48 fStatus
= B_NO_MEMORY
;
52 fStatus
= fFile
->InitCheck();
56 memset(&fFormat
, 0, sizeof(fFormat
));
57 fTrackReader
= new(std::nothrow
) BPrivate::BTrackReader(fFile
, fFormat
);
58 if (fTrackReader
== NULL
) {
59 fStatus
= B_NO_MEMORY
;
63 fStatus
= fTrackReader
->InitCheck();
67 fFormat
= fTrackReader
->Format();
72 BSound::BSound(const media_raw_audio_format
& format
)
82 // unimplemented protected constructor
107 atomic_add(&fRefCount
, 1);
115 if (atomic_add(&fRefCount
, -1) == 1) {
120 // TODO: verify those returns
126 BSound::RefCount() const
133 BSound::Duration() const
140 const media_raw_audio_format
&
141 BSound::Format() const
159 fFile
->GetSize(&result
);
168 BSound::GetDataAt(off_t offset
, void* intoBuffer
, size_t bufferSize
,
171 if (intoBuffer
== NULL
)
175 size_t copySize
= MIN(bufferSize
, fDataSize
- offset
);
176 memcpy(intoBuffer
, (uint8
*)fData
+ offset
, copySize
);
182 if (fTrackReader
!= NULL
) {
183 int32 frameSize
= fTrackReader
->FrameSize();
184 int64 frameCount
= fTrackReader
->CountFrames();
185 int64 startFrame
= offset
/ frameSize
;
186 if (startFrame
> frameCount
)
189 if (fTrackReader
->SeekToFrame(&startFrame
) != B_OK
)
192 off_t bufferOffset
= offset
- startFrame
* frameSize
;
193 int64 directStartFrame
= (offset
+ frameSize
- 1) / frameSize
;
194 int64 directFrameCount
= (offset
+ bufferSize
- directStartFrame
195 * frameSize
) / frameSize
;
197 if (bufferOffset
!= 0) {
198 int64 indirectFrameCount
= directStartFrame
- startFrame
;
199 size_t indirectSize
= indirectFrameCount
* frameSize
;
200 void* buffer
= malloc(indirectSize
);
204 if (fTrackReader
->ReadFrames(buffer
, indirectFrameCount
) != B_OK
) {
209 memcpy(intoBuffer
, (uint8
*)buffer
+ bufferOffset
,
210 indirectSize
- bufferOffset
);
212 *outUsed
= indirectSize
- bufferOffset
;
215 } else if (outUsed
!= NULL
)
218 if (fTrackReader
->ReadFrames((uint8
*)intoBuffer
+ bufferOffset
,
219 directFrameCount
) != B_OK
)
223 *outUsed
+= directFrameCount
* frameSize
;
233 BSound::BindTo(BSoundPlayer
* player
, const media_raw_audio_format
& format
)
241 BSound::UnbindFrom(BSoundPlayer
* player
)
249 BSound::Perform(int32 code
, ...)
256 status_t
BSound::_Reserved_Sound_0(void*) { return B_ERROR
; }
257 status_t
BSound::_Reserved_Sound_1(void*) { return B_ERROR
; }
258 status_t
BSound::_Reserved_Sound_2(void*) { return B_ERROR
; }
259 status_t
BSound::_Reserved_Sound_3(void*) { return B_ERROR
; }
260 status_t
BSound::_Reserved_Sound_4(void*) { return B_ERROR
; }
261 status_t
BSound::_Reserved_Sound_5(void*) { return B_ERROR
; }