2 |
* * |
* * |
3 |
* LinuxSampler - modular, streaming capable sampler * |
* LinuxSampler - modular, streaming capable sampler * |
4 |
* * |
* * |
5 |
* Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck * |
* Copyright (C) 2003,2004 by Benno Senoner and Christian Schoenebeck * |
6 |
* Copyright (C) 2005 - 2007 Christian Schoenebeck * |
* Copyright (C) 2005-2009 Christian Schoenebeck * |
7 |
|
* Copyright (C) 2009 Grigor Iliev * |
8 |
* * |
* * |
9 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
10 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
23 |
***************************************************************************/ |
***************************************************************************/ |
24 |
|
|
25 |
#include "Stream.h" |
#include "Stream.h" |
|
|
|
26 |
#include "../../common/global_private.h" |
#include "../../common/global_private.h" |
27 |
|
|
28 |
namespace LinuxSampler { namespace gig { |
namespace LinuxSampler { namespace gig { |
29 |
|
|
30 |
uint Stream::UnusedStreams = 0; |
Stream::Stream ( |
31 |
uint Stream::TotalStreams = 0; |
::gig::buffer_t* pDecompressionBuffer, |
32 |
|
uint BufferSize, |
33 |
/// Returns number of refilled sample points or a value < 0 on error. |
uint BufferWrapElements) : LinuxSampler::StreamBase< ::gig::DimensionRegion>(BufferSize, BufferWrapElements) |
34 |
int Stream::ReadAhead(unsigned long SampleCount) { |
{ |
35 |
if (this->State == state_unused) return -1; |
this->pDecompressionBuffer = pDecompressionBuffer; |
36 |
if (this->State == state_end) return 0; |
} |
|
if (!SampleCount) return 0; |
|
|
if (!pRingBuffer->write_space()) return 0; |
|
37 |
|
|
38 |
::gig::Sample* pSample = pDimRgn->pSample; |
long Stream::Read(uint8_t* pBuf, long SamplesToRead) { |
39 |
|
::gig::Sample* pSample = pRegion->pSample; |
40 |
long total_readsamples = 0, readsamples = 0; |
long total_readsamples = 0, readsamples = 0; |
|
long samplestoread = SampleCount / pSample->Channels; |
|
|
uint8_t* pBuf = pRingBuffer->get_write_ptr(); |
|
41 |
bool endofsamplereached; |
bool endofsamplereached; |
42 |
|
|
43 |
// refill the disk stream buffer |
// refill the disk stream buffer |
44 |
if (this->DoLoop) { // honor looping |
if (this->DoLoop) { // honor looping |
45 |
total_readsamples = pSample->ReadAndLoop(pBuf, samplestoread, &this->PlaybackState, pDimRgn, pDecompressionBuffer); |
::gig::playback_state_t pbs; |
46 |
|
pbs.position = PlaybackState.position; |
47 |
|
pbs.reverse = PlaybackState.reverse; |
48 |
|
pbs.loop_cycles_left = PlaybackState.loop_cycles_left; |
49 |
|
|
50 |
|
total_readsamples = pSample->ReadAndLoop(pBuf, SamplesToRead, &pbs, pRegion, pDecompressionBuffer); |
51 |
|
PlaybackState.position = pbs.position; |
52 |
|
PlaybackState.reverse = pbs.reverse; |
53 |
|
PlaybackState.loop_cycles_left = pbs.loop_cycles_left; |
54 |
endofsamplereached = (this->PlaybackState.position >= pSample->SamplesTotal); |
endofsamplereached = (this->PlaybackState.position >= pSample->SamplesTotal); |
55 |
dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->PlaybackState.position)); |
dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->PlaybackState.position)); |
56 |
} |
} |
59 |
pSample->SetPos(this->SampleOffset); // recover old position |
pSample->SetPos(this->SampleOffset); // recover old position |
60 |
|
|
61 |
do { |
do { |
62 |
readsamples = pSample->Read(&pBuf[total_readsamples * pSample->FrameSize], samplestoread, pDecompressionBuffer); |
readsamples = pSample->Read(&pBuf[total_readsamples * pSample->FrameSize], SamplesToRead, pDecompressionBuffer); |
63 |
samplestoread -= readsamples; |
SamplesToRead -= readsamples; |
64 |
total_readsamples += readsamples; |
total_readsamples += readsamples; |
65 |
} while (samplestoread && readsamples > 0); |
} while (SamplesToRead && readsamples > 0); |
66 |
|
|
67 |
// we have to store the position within the sample, because other streams might use the same sample |
// we have to store the position within the sample, because other streams might use the same sample |
68 |
this->SampleOffset = pSample->GetPos(); |
this->SampleOffset = pSample->GetPos(); |
71 |
dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->SampleOffset)); |
dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->SampleOffset)); |
72 |
} |
} |
73 |
|
|
|
// we must delay the increment_write_ptr_with_wrap() after the while() loop because we need to |
|
|
// ensure that we read exactly SampleCount sample, otherwise the buffer wrapping code will fail |
|
|
pRingBuffer->increment_write_ptr_with_wrap(total_readsamples * pSample->FrameSize); |
|
|
|
|
74 |
// update stream state |
// update stream state |
75 |
if (endofsamplereached) SetState(state_end); |
if (endofsamplereached) SetState(state_end); |
76 |
else SetState(state_active); |
else SetState(state_active); |
78 |
return total_readsamples; |
return total_readsamples; |
79 |
} |
} |
80 |
|
|
81 |
void Stream::WriteSilence(unsigned long SilenceSampleWords) { |
void Stream::Launch ( |
82 |
memset(pRingBuffer->get_write_ptr(), 0, SilenceSampleWords * BytesPerSample); |
Stream::Handle hStream, |
83 |
pRingBuffer->increment_write_ptr_with_wrap(SilenceSampleWords * BytesPerSample); |
reference_t* pExportReference, |
84 |
} |
::gig::DimensionRegion* pRgn, |
85 |
|
unsigned long SampleOffset, |
86 |
Stream::Stream( ::gig::buffer_t* pDecompressionBuffer, uint BufferSize, uint BufferWrapElements) { |
bool DoLoop |
87 |
this->pExportReference = NULL; |
) { |
88 |
this->State = state_unused; |
SampleDescription info; |
89 |
this->hThis = 0; |
info.ChannelsPerFrame = pRgn->pSample->Channels; |
90 |
this->pDimRgn = NULL; |
info.FrameSize = pRgn->pSample->FrameSize; |
91 |
this->SampleOffset = 0; |
info.BytesPerSample = pRgn->pSample->BitDepth / 8; |
92 |
this->PlaybackState.position = 0; |
info.TotalSampleCount = pRgn->pSample->SamplesTotal; |
93 |
this->PlaybackState.reverse = false; |
|
94 |
this->pRingBuffer = new RingBuffer<uint8_t,false>(BufferSize * 3, BufferWrapElements * 3); |
Sample::PlaybackState playbackState; |
95 |
this->pDecompressionBuffer = pDecompressionBuffer; |
playbackState.position = SampleOffset; |
96 |
UnusedStreams++; |
playbackState.reverse = false; |
97 |
TotalStreams++; |
playbackState.loop_cycles_left = pRgn->pSample->LoopPlayCount; |
98 |
} |
|
99 |
|
LinuxSampler::StreamBase< ::gig::DimensionRegion>::Launch ( |
100 |
Stream::~Stream() { |
hStream, pExportReference, pRgn, info, playbackState, SampleOffset, DoLoop |
101 |
Reset(); |
); |
|
if (pRingBuffer) delete pRingBuffer; |
|
|
UnusedStreams--; |
|
|
TotalStreams--; |
|
|
} |
|
|
|
|
|
/// Called by disk thread to activate the disk stream. |
|
|
void Stream::Launch(Stream::Handle hStream, reference_t* pExportReference, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop) { |
|
|
UnusedStreams--; |
|
|
this->pExportReference = pExportReference; |
|
|
this->hThis = hStream; |
|
|
this->pDimRgn = pDimRgn; |
|
|
this->SampleOffset = SampleOffset; |
|
|
this->PlaybackState.position = SampleOffset; |
|
|
this->PlaybackState.reverse = false; |
|
|
this->PlaybackState.loop_cycles_left = pDimRgn->pSample->LoopPlayCount; |
|
|
this->DoLoop = DoLoop; |
|
|
BytesPerSample = pDimRgn->pSample->BitDepth / 8; |
|
|
SetState(state_active); |
|
102 |
} |
} |
103 |
|
|
104 |
}} // namespace LinuxSampler::gig |
}} // namespace LinuxSampler::gig |