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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1424 - (hide 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 schoenebeck 53 /***************************************************************************
2     * *
3     * LinuxSampler - modular, streaming capable sampler *
4     * *
5 schoenebeck 56 * Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck *
6 schoenebeck 1424 * Copyright (C) 2005 - 2007 Christian Schoenebeck *
7 schoenebeck 53 * *
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 schoenebeck 1424 #include "../../common/global_private.h"
27    
28 schoenebeck 53 namespace LinuxSampler { namespace gig {
29    
30     uint Stream::UnusedStreams = 0;
31 senkov 333 uint Stream::TotalStreams = 0;
32 schoenebeck 53
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 persson 865 ::gig::Sample* pSample = pDimRgn->pSample;
41 schoenebeck 53 long total_readsamples = 0, readsamples = 0;
42     long samplestoread = SampleCount / pSample->Channels;
43 persson 903 uint8_t* pBuf = pRingBuffer->get_write_ptr();
44 schoenebeck 53 bool endofsamplereached;
45    
46     // refill the disk stream buffer
47     if (this->DoLoop) { // honor looping
48 persson 865 total_readsamples = pSample->ReadAndLoop(pBuf, samplestoread, &this->PlaybackState, pDimRgn, pDecompressionBuffer);
49 schoenebeck 53 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 persson 903 readsamples = pSample->Read(&pBuf[total_readsamples * pSample->FrameSize], samplestoread, pDecompressionBuffer);
58 schoenebeck 53 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 persson 903 pRingBuffer->increment_write_ptr_with_wrap(total_readsamples * pSample->FrameSize);
72 schoenebeck 53
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 persson 903 memset(pRingBuffer->get_write_ptr(), 0, SilenceSampleWords * BytesPerSample);
82     pRingBuffer->increment_write_ptr_with_wrap(SilenceSampleWords * BytesPerSample);
83 schoenebeck 53 }
84    
85 schoenebeck 385 Stream::Stream( ::gig::buffer_t* pDecompressionBuffer, uint BufferSize, uint BufferWrapElements) {
86     this->pExportReference = NULL;
87     this->State = state_unused;
88     this->hThis = 0;
89 persson 865 this->pDimRgn = NULL;
90 schoenebeck 385 this->SampleOffset = 0;
91     this->PlaybackState.position = 0;
92     this->PlaybackState.reverse = false;
93 schoenebeck 970 this->pRingBuffer = new RingBuffer<uint8_t,false>(BufferSize * 3, BufferWrapElements * 3);
94 schoenebeck 385 this->pDecompressionBuffer = pDecompressionBuffer;
95 schoenebeck 53 UnusedStreams++;
96 senkov 333 TotalStreams++;
97 schoenebeck 53 }
98    
99     Stream::~Stream() {
100     Reset();
101     if (pRingBuffer) delete pRingBuffer;
102 senkov 329 UnusedStreams--;
103 senkov 333 TotalStreams--;
104 schoenebeck 53 }
105    
106     /// Called by disk thread to activate the disk stream.
107 persson 865 void Stream::Launch(Stream::Handle hStream, reference_t* pExportReference, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop) {
108 schoenebeck 53 UnusedStreams--;
109     this->pExportReference = pExportReference;
110     this->hThis = hStream;
111 persson 865 this->pDimRgn = pDimRgn;
112 schoenebeck 53 this->SampleOffset = SampleOffset;
113     this->PlaybackState.position = SampleOffset;
114     this->PlaybackState.reverse = false;
115 persson 865 this->PlaybackState.loop_cycles_left = pDimRgn->pSample->LoopPlayCount;
116 schoenebeck 53 this->DoLoop = DoLoop;
117 persson 903 BytesPerSample = pDimRgn->pSample->BitDepth / 8;
118 schoenebeck 53 SetState(state_active);
119     }
120    
121     }} // namespace LinuxSampler::gig

  ViewVC Help
Powered by ViewVC