39 |
|
|
40 |
long total_readsamples = 0, readsamples = 0; |
long total_readsamples = 0, readsamples = 0; |
41 |
long samplestoread = SampleCount / pSample->Channels; |
long samplestoread = SampleCount / pSample->Channels; |
42 |
|
sample_t* pBuf = pRingBuffer->get_write_ptr(); |
43 |
|
bool endofsamplereached; |
44 |
|
|
45 |
// refill the disk stream buffer |
// refill the disk stream buffer |
46 |
pSample->SetPos(this->SampleOffset); |
if (this->DoLoop) { // honor looping |
47 |
|
total_readsamples = pSample->ReadAndLoop(pBuf, samplestoread, &this->PlaybackState); |
48 |
sample_t* ptr = pRingBuffer->get_write_ptr(); |
endofsamplereached = (this->PlaybackState.position >= pSample->SamplesTotal); |
49 |
do { |
dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->PlaybackState.position)); |
50 |
readsamples = pSample->Read(&ptr[total_readsamples * pSample->Channels], samplestoread); |
} |
51 |
samplestoread -= readsamples; |
else { // normal forward playback |
52 |
total_readsamples += readsamples; |
|
53 |
} while (samplestoread && readsamples > 0); |
pSample->SetPos(this->SampleOffset); // recover old position |
54 |
|
|
55 |
|
do { |
56 |
|
readsamples = pSample->Read(&pBuf[total_readsamples * pSample->Channels], samplestoread); |
57 |
|
samplestoread -= readsamples; |
58 |
|
total_readsamples += readsamples; |
59 |
|
} while (samplestoread && readsamples > 0); |
60 |
|
|
61 |
|
// we have to store the position within the sample, because other streams might use the same sample |
62 |
|
this->SampleOffset = pSample->GetPos(); |
63 |
|
|
64 |
|
endofsamplereached = (SampleOffset >= pSample->SamplesTotal); |
65 |
|
dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->SampleOffset)); |
66 |
|
} |
67 |
|
|
68 |
// we must delay the increment_write_ptr_with_wrap() after the while() loop because we need to |
// we must delay the increment_write_ptr_with_wrap() after the while() loop because we need to |
69 |
// ensure that we read exactly SampleCount sample, otherwise the buffer wrapping code will fail |
// ensure that we read exactly SampleCount sample, otherwise the buffer wrapping code will fail |
70 |
pRingBuffer->increment_write_ptr_with_wrap(total_readsamples * pSample->Channels); |
pRingBuffer->increment_write_ptr_with_wrap(total_readsamples * pSample->Channels); |
71 |
|
|
|
// we have to store the position within the sample, because other streams might use the same sample |
|
|
this->SampleOffset = pSample->GetPos(); |
|
|
|
|
|
bool endofsamplereached = (SampleOffset >= pSample->SamplesTotal); |
|
|
|
|
72 |
// update stream state |
// update stream state |
73 |
if (endofsamplereached) SetState(state_end); |
if (endofsamplereached) SetState(state_end); |
74 |
else SetState(state_active); |
else SetState(state_active); |
75 |
|
|
76 |
dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, SampleCount - samplestoread, this->SampleOffset)); |
return total_readsamples; |
|
return (SampleCount - samplestoread); |
|
77 |
} |
} |
78 |
|
|
79 |
void Stream::WriteSilence(unsigned long SilenceSampleWords) { |
void Stream::WriteSilence(unsigned long SilenceSampleWords) { |
82 |
} |
} |
83 |
|
|
84 |
Stream::Stream(uint BufferSize, uint BufferWrapElements) { |
Stream::Stream(uint BufferSize, uint BufferWrapElements) { |
85 |
this->pExportReference = NULL; |
this->pExportReference = NULL; |
86 |
this->State = state_unused; |
this->State = state_unused; |
87 |
this->hThis = 0; |
this->hThis = 0; |
88 |
this->pSample = NULL; |
this->pSample = NULL; |
89 |
this->SampleOffset = 0; |
this->SampleOffset = 0; |
90 |
this->pRingBuffer = new RingBuffer<sample_t>(BufferSize, BufferWrapElements); |
this->PlaybackState.position = 0; |
91 |
|
this->PlaybackState.reverse = false; |
92 |
|
this->pRingBuffer = new RingBuffer<sample_t>(BufferSize, BufferWrapElements); |
93 |
UnusedStreams++; |
UnusedStreams++; |
94 |
} |
} |
95 |
|
|
99 |
} |
} |
100 |
|
|
101 |
/// Called by disk thread to activate the disk stream. |
/// Called by disk thread to activate the disk stream. |
102 |
void Stream::Launch(Stream::Handle hStream, reference_t* pExportReference, gig::Sample* pSample, unsigned long SampleOffset) { |
void Stream::Launch(Stream::Handle hStream, reference_t* pExportReference, gig::Sample* pSample, unsigned long SampleOffset, bool DoLoop) { |
103 |
UnusedStreams--; |
UnusedStreams--; |
104 |
this->pExportReference = pExportReference; |
this->pExportReference = pExportReference; |
105 |
this->hThis = hStream; |
this->hThis = hStream; |
106 |
this->pSample = pSample; |
this->pSample = pSample; |
107 |
this->SampleOffset = SampleOffset; |
this->SampleOffset = SampleOffset; |
108 |
|
this->PlaybackState.position = SampleOffset; |
109 |
|
this->PlaybackState.reverse = false; |
110 |
|
this->PlaybackState.loop_cycles_left = pSample->LoopPlayCount; |
111 |
|
this->DoLoop = DoLoop; |
112 |
SetState(state_active); |
SetState(state_active); |
113 |
} |
} |