File: audioio-buffered.cpp

package info (click to toggle)
ecasound 2.9.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 6,292 kB
  • sloc: cpp: 39,475; sh: 4,335; lisp: 1,918; ansic: 1,883; makefile: 888; python: 617; ruby: 202
file content (136 lines) | stat: -rw-r--r-- 4,066 bytes parent folder | download | duplicates (6)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// ------------------------------------------------------------------------
// audioio-buffered.cpp: A lower level interface for audio I/O objects
// Copyright (C) 1999-2002,2008,2009 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 <cmath> /* ceil() */
#include <kvu_dbc.h>

#include "eca-logger.h"
#include "samplebuffer.h"
#include "audioio-buffered.h"

AUDIO_IO_BUFFERED::AUDIO_IO_BUFFERED(void) 
  : buffersize_rep(0),
    iobuf_uchar_repp(0),
    iobuf_size_rep(0)
{
}

AUDIO_IO_BUFFERED::~AUDIO_IO_BUFFERED(void)
{ 
  if (iobuf_uchar_repp != 0) { 
    delete[] iobuf_uchar_repp; 
    iobuf_uchar_repp = 0;
    iobuf_size_rep = 0;
  }
}

void AUDIO_IO_BUFFERED::reserve_buffer_space(long int bytes)
{
  if (bytes > static_cast<long int>(iobuf_size_rep)) {
    if (iobuf_uchar_repp != 0) {
      delete[] iobuf_uchar_repp;
      iobuf_uchar_repp = 0;
    }
    // remember to include <iostream>
    // std::cerr << "Reserving " << bytes << " bytes (" << label() << ").\n";
    iobuf_uchar_repp = new unsigned char [bytes];
    iobuf_size_rep = bytes;
  }
}

void AUDIO_IO_BUFFERED::set_buffersize(long int samples)
{
  if (buffersize_rep != samples ||
      static_cast<long int>(iobuf_size_rep) < buffersize_rep * frame_size()) {
    buffersize_rep = samples;
    reserve_buffer_space(buffersize_rep * frame_size());
  }
}

void AUDIO_IO_BUFFERED::read_buffer(SAMPLE_BUFFER* sbuf)
{
  // --------
  DBC_REQUIRE(iobuf_uchar_repp != 0);
  DBC_REQUIRE(static_cast<long int>(iobuf_size_rep) >= buffersize_rep * frame_size());
  // --------

  if (interleaved_channels() == true) {
    sbuf->import_interleaved(iobuf_uchar_repp,
			     read_samples(iobuf_uchar_repp, buffersize_rep),
			     sample_format(),
			     channels());
  }
  else {
    sbuf->import_noninterleaved(iobuf_uchar_repp,
				read_samples(iobuf_uchar_repp, buffersize_rep),
				sample_format(),
				channels());
  }

  if (sbuf->length_in_samples() < buffersize_rep) {
    ECA_LOG_MSG(ECA_LOGGER::user_objects, "end-of-stream tag detected for '"
		+ description() + "'");
    sbuf->event_tag_set(SAMPLE_BUFFER::tag_end_of_stream);
  }

  change_position_in_samples(sbuf->length_in_samples());

  // --------
  DBC_ENSURE(sbuf->number_of_channels() == channels());
  // --------
}

void AUDIO_IO_BUFFERED::write_buffer(SAMPLE_BUFFER* sbuf)
{
  // --------
  DBC_REQUIRE(iobuf_uchar_repp != 0);
  DBC_REQUIRE(static_cast<long int>(iobuf_size_rep) >= buffersize_rep * frame_size());
  // --------

  set_buffersize(sbuf->length_in_samples());

  if (interleaved_channels() == true) {
    sbuf->export_interleaved(iobuf_uchar_repp,
			     sample_format(),
			     channels());
  }
  else {
    sbuf->export_noninterleaved(iobuf_uchar_repp,
				sample_format(),
				channels());
  }

  write_samples(iobuf_uchar_repp, sbuf->length_in_samples());
  change_position_in_samples(sbuf->length_in_samples());
  extend_position();
}

void AUDIO_IO_BUFFERED::set_channels(SAMPLE_SPECS::channel_t v)
{
  AUDIO_IO::set_channels(v);
  /* adjust allocated buffers if audio format has changed */
  set_buffersize(buffersize_rep);
}

void AUDIO_IO_BUFFERED::set_sample_format(Sample_format v) throw(ECA_ERROR&)
{
  AUDIO_IO::set_sample_format(v);
  /* adjust allocated buffers if audio format has changed */
  set_buffersize(buffersize_rep);
}