/[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 1424 - (show annotations) (download)
Sun Oct 14 22:00:17 2007 UTC (16 years, 6 months ago) by schoenebeck
File size: 6082 byte(s)
* code cleanup:
- global.h now only covers global definitions that are needed for the C++
  API header files, all implementation internal global definitions are now
  in global_private.h
- atomic.h is not exposed to the C++ API anymore (replaced the references
  in SynchronizedConfig.h for this with local definitions)
- no need to include config.h anymore for using LS's API header files
- DB instruments classes are not exposed to the C++ API
- POSIX callback functions of Thread.h are hidden
- the (optional) gig Engine benchmark compiles again
- updated Doxyfile.in
- fixed warnings in API doc generation
* preparations for release 0.5.0

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

  ViewVC Help
Powered by ViewVC