File: audioio-db-buffer.cpp

package info (click to toggle)
ecasound2.2 2.4.6.1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,884 kB
  • ctags: 7,401
  • sloc: cpp: 33,779; sh: 8,793; ansic: 1,938; lisp: 1,884; makefile: 983; python: 607; ruby: 202
file content (121 lines) | stat: -rw-r--r-- 3,359 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// ------------------------------------------------------------------------
// audioio-db-buffer.cpp: Buffer used between db server and client
// Copyright (C) 2000-2002 Kai Vehmanen
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
// ------------------------------------------------------------------------

#include "samplebuffer.h"
#include "audioio-db-buffer.h"

/**
 * Constructor.
 */
AUDIO_IO_DB_BUFFER::AUDIO_IO_DB_BUFFER(int number_of_buffers,
				       long int buffersize,
				       int channels)
  :  readptr_rep(0),
     writeptr_rep(0),
     finished_rep(0),
     sbufs_rep(number_of_buffers)
{

  for(size_t n = 0; n < sbufs_rep.size(); n++) {
    sbufs_rep[n] = new SAMPLE_BUFFER(buffersize, channels);
  }

  // std::cerr << "Created a new client buffer with "  << sbufs_rep.size() << " buffers." << std::endl;
}

AUDIO_IO_DB_BUFFER::~AUDIO_IO_DB_BUFFER(void)
{
  for(size_t n = 0; n < sbufs_rep.size(); n++) {
    delete sbufs_rep[n];
  }
}

/**
 * Resets all pointers to their initial state. 
 */
void AUDIO_IO_DB_BUFFER::reset(void)
{
  readptr_rep.set(0);
  writeptr_rep.set(0);
  finished_rep.set(0);
}

/**
 * Number of sample buffer available for reading.
 *
 * Note! As concurrent access is allowed by
 *       the implement, the actual number of 
 *       samples available for reading can
 *       be greater than that reported by 
 *       read_space().
 */
int AUDIO_IO_DB_BUFFER::read_space(void)
{
  int write = writeptr_rep.get();
  int read = readptr_rep.get();

  if (write >= read)
    return(write - read);
  else
    return((write - read + sbufs_rep.size()) % sbufs_rep.size());
}

/**
 * Number of sample buffers available for writing.
 *
 * Note! As concurrent access is allowed by
 *       the implement, the actual number of 
 *       samples available for writing can
 *       be greater than that reported by 
 *       write_space().
 */
int AUDIO_IO_DB_BUFFER::write_space(void)
{
  int write = writeptr_rep.get();
  int read = readptr_rep.get();
  
  if (write > read)
    return(((read - write + sbufs_rep.size()) % sbufs_rep.size()) - 1);
  else if (write < read)
    return(read - write - 1);
  else 
    return(sbufs_rep.size() - 1);
}

/**
 * Increment the read pointer by one.
 *
 * Note! Cannot be called from multiple concurrent
 *       execution contexts.
 **/
void AUDIO_IO_DB_BUFFER::advance_read_pointer(void)
{
  readptr_rep.set((readptr_rep.get() + 1) % sbufs_rep.size());
}

/**
 * Increment the write pointer by one.
 *
 * Note! Cannot be called from multiple concurrent
 *       execution contexts.
 **/
void AUDIO_IO_DB_BUFFER::advance_write_pointer(void)
{
  writeptr_rep.set((writeptr_rep.get() + 1) % sbufs_rep.size());
}