2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2 of the License, or (at your option)
9 This program is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <ardour/buffer.h>
26 Buffer::create(DataType type, size_t capacity)
28 if (type == DataType::AUDIO)
29 return new AudioBuffer(capacity);
30 else if (type == DataType::MIDI)
31 return new MidiBuffer(capacity);
37 AudioBuffer::AudioBuffer(size_t capacity)
38 : Buffer(DataType::AUDIO, capacity)
41 _size = capacity; // For audio buffers, size = capacity (always)
43 #ifdef NO_POSIX_MEMALIGN
44 _data = (Sample *) malloc(sizeof(Sample) * capacity);
46 posix_memalign((void**)&_data, 16, sizeof(Sample) * capacity);
56 AudioBuffer::~AudioBuffer()
62 // FIXME: mirroring for MIDI buffers?
63 MidiBuffer::MidiBuffer(size_t capacity)
64 : Buffer(DataType::MIDI, capacity)
70 _size = capacity; // For audio buffers, size = capacity (always)
71 #ifdef NO_POSIX_MEMALIGN
72 _data = (RawMidi *) malloc(sizeof(RawMidi) * capacity);
74 posix_memalign((void**)&_data, 16, sizeof(RawMidi) * capacity);
77 memset(_data, 0, sizeof(RawMidi) * capacity);
80 MidiBuffer::~MidiBuffer()
86 /** Note that offset and nframes refer to sample time, not actual buffer locations */
88 MidiBuffer::write(const Buffer& src, jack_nframes_t offset, jack_nframes_t nframes)
90 assert(src.type() == DataType::MIDI);
92 MidiBuffer& msrc = (MidiBuffer&)src;
94 for (size_t i=0; i < msrc.size() && msrc.data()[i].time < nframes; ++i) {
95 assert(i < _capacity);
96 _data[i] = msrc.data()[i];
99 assert(_size == msrc.size());
102 std::cerr << "MidiBuffer wrote " << _size << " events.\n";
106 } // namespace ARDOUR