Update NTK.
[nondaw.git] / timeline / src / Engine / Disk_Stream.H
blobfe63524de1739e164407ee0c24b9ad2160a5aa0d
2 /*******************************************************************************/
3 /* Copyright (C) 2008 Jonathan Moore Liles                                     */
4 /*                                                                             */
5 /* This program is free software; you can redistribute it and/or modify it     */
6 /* under the terms of the GNU General Public License as published by the       */
7 /* Free Software Foundation; either version 2 of the License, or (at your      */
8 /* option) any later version.                                                  */
9 /*                                                                             */
10 /* This program is distributed in the hope that it will be useful, but WITHOUT */
11 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       */
12 /* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   */
13 /* more details.                                                               */
14 /*                                                                             */
15 /* You should have received a copy of the GNU General Public License along     */
16 /* with This program; see the file COPYING.  If not,write to the Free Software */
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18 /*******************************************************************************/
20 #pragma once
23 #include <jack/ringbuffer.h>
24 #include <semaphore.h>
25 #include <errno.h>
27 #include <vector>
29 #include "types.h"
30 #include "Mutex.H"
31 #include "const.h"
32 #include "debug.h"
33 #include "Thread.H"
35 class Track;
36 class Audio_Sequence;
38 class Disk_Stream : public Mutex
41     /* not permitted */
42     Disk_Stream ( const Disk_Stream &rhs );
43     Disk_Stream & operator = ( const Disk_Stream &rhs );
46 protected:
48     Thread _thread;                                    /* io thread */
50     Track *_track;                               /* Track we belong to */
52     nframes_t _nframes;                              /* buffer size */
55     std::vector < jack_ringbuffer_t * >_rb; /* one ringbuffer for each channel */
57     sem_t _blocks;          /* semaphore to wake the IO thread with */
59     int _total_blocks; /* total number of blocks that we can  buffer */
60     int _disk_io_blocks; /* the number of blocks to read/write to/from disk at once */
63     nframes_t _frame_rate;      /* used for buffer size calculations */
65     volatile nframes_t _frame;             /* location of disk read */
66     volatile nframes_t _pending_seek; /* absolute transport position to seek to */
67     volatile int _terminate;
69     volatile int _xruns;
71     int channels ( void ) const { return _rb.size(); }
73     Audio_Sequence * sequence ( void ) const;
74     Track * track ( void ) const;
76     static void *disk_thread ( void *arg );
78     void _resize_buffers ( nframes_t nframes, int channels );
80 protected:
82     void block_processed ( void ) { sem_post( &_blocks ); }
83     bool wait_for_block ( void )
84         {
85             while ( ! sem_wait( &_blocks ) && errno == EINTR )
86             {}
88             return ! _terminate;
89         }
91     virtual void disk_thread ( void ) = 0;
93     void base_flush ( bool is_output );
94     virtual void flush ( void ) = 0;
96     void run ( void );
97     void detach ( void );
99 public:
101     void shutdown ( void );
103     /* must be set before any Disk_Streams are created */
104     static float seconds_to_buffer;
105     static size_t disk_io_kbytes;
107     int xruns ( void ) { return _xruns; }
109     Disk_Stream ( Track *th, float frame_rate, nframes_t nframes, int channels );
111     virtual ~Disk_Stream ( );
113     void resize_buffers ( nframes_t nframes );
115 /*     void seek ( nframes_t frame ); */
116 /*     bool seek_pending ( void ); */
118     virtual nframes_t process ( nframes_t nframes ) = 0;
120     int buffer_percent ( void );