/[svn]/linuxsampler/trunk/src/stream.h
ViewVC logotype

Contents of /linuxsampler/trunk/src/stream.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (show annotations) (download) (as text)
Sun Dec 7 05:03:43 2003 UTC (20 years, 4 months ago) by schoenebeck
File MIME type: text/x-c++hdr
File size: 7205 byte(s)
* src/audioio.cpp: added support for Alsa 1.0.0
* src/audiothread.cpp: fixed several bugs in sustain pedal handling
* src/diskthread.cpp: fixed several bugs which occured under extreme
  conditions (endless loop in audiothread, freezing the whole application,
  outage of available disk streams)
* src/voice.cpp: fixed cubic interpolation (disabled by default; you can
  enable it by setting USE_LINEAR_INTERPOLATION to 0 in src/voice.h)
* src/configure.in: added check for Alsa version

1 /***************************************************************************
2 * *
3 * LinuxSampler - modular, streaming capable sampler *
4 * *
5 * Copyright (C) 2003 by Benno Senoner *
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 #ifndef __STREAM_H__
24 #define __STREAM_H__
25
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/mman.h>
31 #include <asm/page.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/stat.h>
35
36 #include "global.h"
37 #include "ringbuffer.h"
38 #include "gig.h"
39
40 class Stream {
41 public:
42 // Member Types
43 typedef uint32_t OrderID_t;
44 typedef uint32_t Handle; ///< unique identifier of a relationship between one stream and a consumer (Voice)
45 enum state_t { ///< streams go through severe cyclic state transition (unused->active->end->unused->...)
46 state_unused, ///< stream is not in use, thus can still be launched
47 state_active, ///< stream provides data in it's buffer to be read and hasn't reached the end yet (this is the usual case)
48 state_end ///< stream end reached but still providing data in it's buffer to be read (after the client read all remaining data from the stream buffer, state will change automatically to state_unused)
49 };
50 struct reference_t { ///< Defines the current relationship between the stream and a client (voice).
51 OrderID_t OrderID; ///< Unique identifier that identifies the creation order of a stream requested by a voice.
52 Handle hStream; ///< Unique identifier of the relationship between stream and client.
53 state_t State; ///< Current state of the stream that will be pretended to the client (the actual state of the stream might differ though, because the stream might already be in use by another client).
54 Stream* pStream; ///< Points to the assigned and activated stream or is NULL if the disk thread hasn't launched a stream yet.
55 };
56
57 // Methods
58 Stream(uint BufferSize, uint BufferWrapElements);
59 ~Stream();
60 int ReadAhead(unsigned long SampleCount);
61 void WriteSilence(unsigned long SilenceSampleWords);
62
63 inline int GetReadSpace() {
64 return (pRingBuffer && State == state_active) ? pRingBuffer->read_space() : 0;
65 }
66
67 inline int GetWriteSpace() {
68 return (pRingBuffer && State == state_active) ? pRingBuffer->write_space() : 0;
69 }
70
71 inline int GetWriteSpaceToEnd() {
72 return (pRingBuffer && State == state_active) ? pRingBuffer->write_space_to_end_with_wrap() : 0;
73 }
74
75 // adjusts the write space to avoid buffer boundaries which would lead to the wrap space
76 // within the buffer (needed for interpolation) getting filled only partially
77 // for more infos see the docs in ringbuffer.h at adjust_write_space_to_avoid_boundary()
78 inline int AdjustWriteSpaceToAvoidBoundary(int cnt, int capped_cnt) {
79 return pRingBuffer->adjust_write_space_to_avoid_boundary(cnt, capped_cnt);
80 }
81
82 inline sample_t* GetReadPointer() {
83 return pRingBuffer->get_read_ptr();
84 }
85
86 // gets the current read_ptr within the ringbuffer
87 inline sample_t* GetReadPtr(void) {
88 return pRingBuffer->get_read_ptr();
89 }
90
91 inline void IncrementReadPos(uint Count) {
92 uint leftspace = pRingBuffer->read_space();
93 pRingBuffer->increment_read_ptr(Min(Count, leftspace));
94 if (State == state_end && Count >= leftspace) {
95 Reset(); // quit relation between consumer (voice) and stream and reset stream right after
96 }
97 }
98
99 // Static Method
100 inline static uint GetUnusedStreams() { return UnusedStreams; }
101 protected:
102 // Methods
103 void Launch(Stream::Handle hStream, reference_t* pExportReference, gig::Sample* pSample, unsigned long SampleOffset);
104 inline void Kill() { pExportReference = NULL; Reset(); } ///< Will be called by disk thread after a 'deletion' command from the audio thread (within the voice class)
105 inline Stream::Handle GetHandle() { return hThis; }
106 inline Stream::state_t GetState() { return State; }
107 friend class DiskThread; // only the disk thread should be able to launch and most important kill a disk stream to avoid race conditions
108 private:
109 // Attributes
110 reference_t* pExportReference;
111 state_t State;
112 Stream::Handle hThis;
113 gig::Sample* pSample;
114 unsigned long SampleOffset;
115 RingBuffer<sample_t>* pRingBuffer;
116
117 // Static Attributes
118 static uint UnusedStreams; //< Reflects how many stream objects of all stream instances are currently not in use.
119
120 // Methods
121 inline void Reset() {
122 pSample = NULL;
123 SampleOffset = 0;
124 hThis = 0;
125 pRingBuffer->init(); // reset ringbuffer
126 if (State != state_unused) {
127 // we can't do 'SetPos(state_unused)' here, due to possible race conditions)
128 if (pExportReference) {
129 pExportReference->State = state_unused;
130 pExportReference = NULL;
131 }
132 State = state_unused;
133 UnusedStreams++;
134 }
135 }
136 inline void SetState(state_t State) {
137 if (pExportReference) pExportReference->State = State;
138 this->State = State;
139 }
140 inline long Min(long a, long b) { return (a < b) ? a : b; }
141 };
142
143 #endif // __STREAM_H__

  ViewVC Help
Powered by ViewVC