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

Diff of /linuxsampler/trunk/src/engines/gig/DiskThread.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 53 by schoenebeck, Mon Apr 26 17:15:51 2004 UTC revision 865 by persson, Sun May 14 07:15:52 2006 UTC
# Line 2  Line 2 
2   *                                                                         *   *                                                                         *
3   *   LinuxSampler - modular, streaming capable sampler                     *   *   LinuxSampler - modular, streaming capable sampler                     *
4   *                                                                         *   *                                                                         *
5   *   Copyright (C) 2003 by Benno Senoner                                   *   *   Copyright (C) 2003, 2004 by Benno Senoner and Christian Schoenebeck   *
6     *   Copyright (C) 2005 Christian Schoenebeck                              *
7   *                                                                         *   *                                                                         *
8   *   This program is free software; you can redistribute it and/or modify  *   *   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  *   *   it under the terms of the GNU General Public License as published by  *
# Line 48  namespace LinuxSampler { namespace gig { Line 49  namespace LinuxSampler { namespace gig {
49      void DiskThread::Reset() {      void DiskThread::Reset() {
50          bool running = this->IsRunning();          bool running = this->IsRunning();
51          if (running) this->StopThread();          if (running) this->StopThread();
52          for (int i = 0; i < MAX_INPUT_STREAMS; i++) {          for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
53              pStreams[i]->Kill();              pStreams[i]->Kill();
54          }          }
55          for (int i = 1; i <= MAX_INPUT_STREAMS; i++) {          for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) {
56              pCreatedStreams[i] = NULL;              pCreatedStreams[i] = NULL;
57          }          }
58          GhostQueue->init();          GhostQueue->init();
# Line 85  namespace LinuxSampler { namespace gig { Line 86  namespace LinuxSampler { namespace gig {
86          std::stringstream ss;          std::stringstream ss;
87          for (uint i = 0; i < this->Streams; i++) {          for (uint i = 0; i < this->Streams; i++) {
88              if (pStreams[i]->GetState() == Stream::state_unused) continue;              if (pStreams[i]->GetState() == Stream::state_unused) continue;
89              uint bufferfill = (uint) ((float) pStreams[i]->GetReadSpace() / (float) STREAM_BUFFER_SIZE * 100);              uint bufferfill = (uint) ((float) pStreams[i]->GetReadSpace() / (float) CONFIG_STREAM_BUFFER_SIZE * 100);
90              uint streamid   = (uint) pStreams[i]->GetHandle();              uint streamid   = (uint) pStreams[i]->GetHandle();
91              if (!streamid) continue;              if (!streamid) continue;
92    
# Line 102  namespace LinuxSampler { namespace gig { Line 103  namespace LinuxSampler { namespace gig {
103       * Returns -1 if command queue or pickup pool is full, 0 on success (will be       * Returns -1 if command queue or pickup pool is full, 0 on success (will be
104       * called by audio thread within the voice class).       * called by audio thread within the voice class).
105       */       */
106      int DiskThread::OrderNewStream(Stream::reference_t* pStreamRef, ::gig::Sample* pSample, unsigned long SampleOffset, bool DoLoop) {      int DiskThread::OrderNewStream(Stream::reference_t* pStreamRef, ::gig::DimensionRegion* pDimRgn, unsigned long SampleOffset, bool DoLoop) {
107          dmsg(4,("Disk Thread: new stream ordered\n"));          dmsg(4,("Disk Thread: new stream ordered\n"));
108          if (CreationQueue->write_space() < 1) {          if (CreationQueue->write_space() < 1) {
109              dmsg(1,("DiskThread: Order queue full!\n"));              dmsg(1,("DiskThread: Order queue full!\n"));
110              return -1;              return -1;
111          }          }
112    
113            const Stream::OrderID_t newOrder = CreateOrderID();
114            if (!newOrder) {
115                    dmsg(1,("DiskThread: there was no free slot\n"));
116                    return -1; // there was no free slot
117            }
118    
119          pStreamRef->State   = Stream::state_active;          pStreamRef->State   = Stream::state_active;
120          pStreamRef->OrderID = CreateOrderID();          pStreamRef->OrderID = newOrder;
121          pStreamRef->hStream = CreateHandle();          pStreamRef->hStream = CreateHandle();
122          pStreamRef->pStream = NULL; // a stream has to be activated by the disk thread first          pStreamRef->pStream = NULL; // a stream has to be activated by the disk thread first
123    
         if (!pStreamRef->OrderID) return -1; // there was no free slot  
   
124          create_command_t cmd;          create_command_t cmd;
125          cmd.OrderID      = pStreamRef->OrderID;          cmd.OrderID      = pStreamRef->OrderID;
126          cmd.hStream      = pStreamRef->hStream;          cmd.hStream      = pStreamRef->hStream;
127          cmd.pStreamRef   = pStreamRef;          cmd.pStreamRef   = pStreamRef;
128          cmd.pSample      = pSample;          cmd.pDimRgn      = pDimRgn;
129          cmd.SampleOffset = SampleOffset;          cmd.SampleOffset = SampleOffset;
130          cmd.DoLoop       = DoLoop;          cmd.DoLoop       = DoLoop;
131    
# Line 180  namespace LinuxSampler { namespace gig { Line 185  namespace LinuxSampler { namespace gig {
185      // #         (following code should only be executed by the disk thread)      // #         (following code should only be executed by the disk thread)
186    
187    
188      DiskThread::DiskThread(uint BufferWrapElements) : Thread(false, 1, -2) {      DiskThread::DiskThread(uint BufferWrapElements) : Thread(true, false, 1, -2) {
189            DecompressionBuffer = ::gig::Sample::CreateDecompressionBuffer(CONFIG_STREAM_MAX_REFILL_SIZE);
190          CreationQueue       = new RingBuffer<create_command_t>(1024);          CreationQueue       = new RingBuffer<create_command_t>(1024);
191          DeletionQueue       = new RingBuffer<delete_command_t>(1024);          DeletionQueue       = new RingBuffer<delete_command_t>(1024);
192          GhostQueue          = new RingBuffer<Stream::Handle>(MAX_INPUT_STREAMS);          GhostQueue          = new RingBuffer<Stream::Handle>(CONFIG_MAX_STREAMS);
193          Streams             = MAX_INPUT_STREAMS;          Streams             = CONFIG_MAX_STREAMS;
194          RefillStreamsPerRun = REFILL_STREAMS_PER_RUN;          RefillStreamsPerRun = CONFIG_REFILL_STREAMS_PER_RUN;
195          for (int i = 0; i < MAX_INPUT_STREAMS; i++) {          for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
196              pStreams[i] = new Stream(STREAM_BUFFER_SIZE, BufferWrapElements); // 131072 sample words              pStreams[i] = new Stream(&DecompressionBuffer, CONFIG_STREAM_BUFFER_SIZE, BufferWrapElements); // 131072 sample words
197          }          }
198          for (int i = 1; i <= MAX_INPUT_STREAMS; i++) {          for (int i = 1; i <= CONFIG_MAX_STREAMS; i++) {
199              pCreatedStreams[i] = NULL;              pCreatedStreams[i] = NULL;
200          }          }
201            ActiveStreamCountMax = 0;
202      }      }
203    
204      DiskThread::~DiskThread() {      DiskThread::~DiskThread() {
205          for (int i = 0; i < MAX_INPUT_STREAMS; i++) {          for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
206              if (pStreams[i]) delete pStreams[i];              if (pStreams[i]) delete pStreams[i];
207          }          }
208          if (CreationQueue) delete CreationQueue;          if (CreationQueue) delete CreationQueue;
209          if (DeletionQueue) delete DeletionQueue;          if (DeletionQueue) delete DeletionQueue;
210          if (GhostQueue)    delete GhostQueue;          if (GhostQueue)    delete GhostQueue;
211            ::gig::Sample::DestroyDecompressionBuffer(DecompressionBuffer);
212      }      }
213    
214      int DiskThread::Main() {      int DiskThread::Main() {
215          dmsg(3,("Disk thread running\n"));          dmsg(3,("Disk thread running\n"));
216          while (true) {          while (true) {
217                pthread_testcancel(); // mandatory for OSX
218              IsIdle = true; // will be set to false if a stream got filled              IsIdle = true; // will be set to false if a stream got filled
219    
220              // if there are ghost streams, delete them              // if there are ghost streams, delete them
# Line 231  namespace LinuxSampler { namespace gig { Line 240  namespace LinuxSampler { namespace gig {
240              }              }
241    
242              // if there are deletion commands, delete those streams              // if there are deletion commands, delete those streams
243              while (Stream::UnusedStreams < Streams && DeletionQueue->read_space() > 0) {              while (Stream::UnusedStreams < Stream::TotalStreams && DeletionQueue->read_space() > 0) {
244                  delete_command_t command;                  delete_command_t command;
245                  DeletionQueue->pop(&command);                  DeletionQueue->pop(&command);
246                  DeleteStream(command);                  DeleteStream(command);
# Line 240  namespace LinuxSampler { namespace gig { Line 249  namespace LinuxSampler { namespace gig {
249              RefillStreams(); // refill the most empty streams              RefillStreams(); // refill the most empty streams
250    
251              // if nothing was done during this iteration (eg no streambuffer              // if nothing was done during this iteration (eg no streambuffer
252              // filled with data) then sleep for 50ms              // filled with data) then sleep for 30ms
253              if (IsIdle) usleep(30000);              if (IsIdle) usleep(30000);
254    
255              int streamsInUsage = 0;              int streamsInUsage = 0;
# Line 267  namespace LinuxSampler { namespace gig { Line 276  namespace LinuxSampler { namespace gig {
276              std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush;              std::cerr << "No unused stream found (OrderID:" << Command.OrderID << ") - report if this happens, this is a bug!\n" << std::flush;
277              return;              return;
278          }          }
279          newstream->Launch(Command.hStream, Command.pStreamRef, Command.pSample, Command.SampleOffset, Command.DoLoop);          newstream->Launch(Command.hStream, Command.pStreamRef, Command.pDimRgn, Command.SampleOffset, Command.DoLoop);
280          dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream));          dmsg(4,("new Stream launched by disk thread (OrderID:%d,StreamHandle:%d)\n", Command.OrderID, Command.hStream));
281          if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {          if (pCreatedStreams[Command.OrderID] != SLOT_RESERVED) {
282              std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush;              std::cerr << "DiskThread: Slot " << Command.OrderID << " already occupied! Please report this!\n" << std::flush;
# Line 313  namespace LinuxSampler { namespace gig { Line 322  namespace LinuxSampler { namespace gig {
322    
323                  int capped_writespace = writespace;                  int capped_writespace = writespace;
324                  // if there is too much buffer space available then cut the read/write                  // if there is too much buffer space available then cut the read/write
325                  // size to MAX_REFILL_SIZE which is by default 65536 samples = 256KBytes                  // size to CONFIG_STREAM_MAX_REFILL_SIZE which is by default 65536 samples = 256KBytes
326                  if (writespace > MAX_REFILL_SIZE) capped_writespace = MAX_REFILL_SIZE;                  if (writespace > CONFIG_STREAM_MAX_REFILL_SIZE) capped_writespace = CONFIG_STREAM_MAX_REFILL_SIZE;
327    
328                  // adjust the amount to read in order to ensure that the buffer wraps correctly                  // adjust the amount to read in order to ensure that the buffer wraps correctly
329                  int read_amount = pStreams[i]->AdjustWriteSpaceToAvoidBoundary(writespace, capped_writespace);                  int read_amount = pStreams[i]->AdjustWriteSpaceToAvoidBoundary(writespace, capped_writespace);
330                  // if we wasn't able to refill one of the stream buffers by more than                  // if we wasn't able to refill one of the stream buffers by more than
331                  // MIN_REFILL_SIZE we'll send the disk thread to sleep later                  // CONFIG_STREAM_MIN_REFILL_SIZE we'll send the disk thread to sleep later
332                  if (pStreams[i]->ReadAhead(read_amount) > MIN_REFILL_SIZE) this->IsIdle = false;                  if (pStreams[i]->ReadAhead(read_amount) > CONFIG_STREAM_MIN_REFILL_SIZE) this->IsIdle = false;
333              }              }
334          }          }
335      }      }
# Line 335  namespace LinuxSampler { namespace gig { Line 344  namespace LinuxSampler { namespace gig {
344    
345      /// order ID Generator      /// order ID Generator
346      Stream::OrderID_t DiskThread::CreateOrderID() {      Stream::OrderID_t DiskThread::CreateOrderID() {
347          static uint32_t counter = 0;          static Stream::OrderID_t counter(0);
348          for (int i = 0; i < MAX_INPUT_STREAMS; i++) {          for (int i = 0; i < CONFIG_MAX_STREAMS; i++) {
349              if (counter == MAX_INPUT_STREAMS) counter = 1; // we use '0' as 'invalid order' only, so we skip 0              if (counter == CONFIG_MAX_STREAMS) counter = 1; // we use '0' as 'invalid order' only, so we skip 0
350              else                              counter++;              else                              counter++;
351              if (!pCreatedStreams[counter]) {              if (!pCreatedStreams[counter]) {
352                  pCreatedStreams[counter] = SLOT_RESERVED; // mark this slot as reserved                  pCreatedStreams[counter] = SLOT_RESERVED; // mark this slot as reserved

Legend:
Removed from v.53  
changed lines
  Added in v.865

  ViewVC Help
Powered by ViewVC