/[svn]/linuxsampler/trunk/src/engines/gig/Stream.cpp
ViewVC logotype

Contents of /linuxsampler/trunk/src/engines/gig/Stream.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 333 - (show annotations) (download)
Sat Jan 1 08:18:07 2005 UTC (19 years, 3 months ago) by senkov
File size: 5677 byte(s)
* Fix a basic diskthread multichannel issue.

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, *
20 * MA 02111-1307 USA *
21 ***************************************************************************/
22
23 #include "Stream.h"
24
25 namespace LinuxSampler { namespace gig {
26
27 uint Stream::UnusedStreams = 0;
28 uint Stream::TotalStreams = 0;
29
30 /// Returns number of refilled sample points or a value < 0 on error.
31 int Stream::ReadAhead(unsigned long SampleCount) {
32 if (this->State == state_unused) return -1;
33 if (this->State == state_end) return 0;
34 if (!SampleCount) return 0;
35 if (!pRingBuffer->write_space()) return 0;
36
37 long total_readsamples = 0, readsamples = 0;
38 long samplestoread = SampleCount / pSample->Channels;
39 sample_t* pBuf = pRingBuffer->get_write_ptr();
40 bool endofsamplereached;
41
42 // refill the disk stream buffer
43 if (this->DoLoop) { // honor looping
44 total_readsamples = pSample->ReadAndLoop(pBuf, samplestoread, &this->PlaybackState);
45 endofsamplereached = (this->PlaybackState.position >= pSample->SamplesTotal);
46 dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->PlaybackState.position));
47 }
48 else { // normal forward playback
49
50 pSample->SetPos(this->SampleOffset); // recover old position
51
52 do {
53 readsamples = pSample->Read(&pBuf[total_readsamples * pSample->Channels], samplestoread);
54 samplestoread -= readsamples;
55 total_readsamples += readsamples;
56 } while (samplestoread && readsamples > 0);
57
58 // we have to store the position within the sample, because other streams might use the same sample
59 this->SampleOffset = pSample->GetPos();
60
61 endofsamplereached = (SampleOffset >= pSample->SamplesTotal);
62 dmsg(5,("Refilled stream %d with %d (SamplePos: %d)", this->hThis, total_readsamples, this->SampleOffset));
63 }
64
65 // we must delay the increment_write_ptr_with_wrap() after the while() loop because we need to
66 // ensure that we read exactly SampleCount sample, otherwise the buffer wrapping code will fail
67 pRingBuffer->increment_write_ptr_with_wrap(total_readsamples * pSample->Channels);
68
69 // update stream state
70 if (endofsamplereached) SetState(state_end);
71 else SetState(state_active);
72
73 return total_readsamples;
74 }
75
76 void Stream::WriteSilence(unsigned long SilenceSampleWords) {
77 memset(pRingBuffer->get_write_ptr(), 0, SilenceSampleWords);
78 pRingBuffer->increment_write_ptr_with_wrap(SilenceSampleWords);
79 }
80
81 Stream::Stream(uint BufferSize, uint BufferWrapElements) {
82 this->pExportReference = NULL;
83 this->State = state_unused;
84 this->hThis = 0;
85 this->pSample = NULL;
86 this->SampleOffset = 0;
87 this->PlaybackState.position = 0;
88 this->PlaybackState.reverse = false;
89 this->pRingBuffer = new RingBuffer<sample_t>(BufferSize, BufferWrapElements);
90 UnusedStreams++;
91 TotalStreams++;
92 }
93
94 Stream::~Stream() {
95 Reset();
96 if (pRingBuffer) delete pRingBuffer;
97 UnusedStreams--;
98 TotalStreams--;
99 }
100
101 /// 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, bool DoLoop) {
103 UnusedStreams--;
104 this->pExportReference = pExportReference;
105 this->hThis = hStream;
106 this->pSample = pSample;
107 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);
113 }
114
115 }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC