- Changed IO's vector<Port*>'s to PortList
[ardour.git] / libs / ardour / buffer.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3     
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)
7     any later version.
8     
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
12     for more details.
13     
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.
17 */
18
19 #include <ardour/buffer.h>
20 #include <iostream>
21
22 namespace ARDOUR {
23
24
25 Buffer*
26 Buffer::create(DataType type, size_t capacity)
27 {
28         if (type == DataType::AUDIO)
29                 return new AudioBuffer(capacity);
30         else if (type == DataType::MIDI)
31                 return new MidiBuffer(capacity);
32         else
33                 return NULL;
34 }
35
36
37 AudioBuffer::AudioBuffer(size_t capacity)
38         : Buffer(DataType::AUDIO, capacity)
39         , _data(NULL)
40 {
41         _size = capacity; // For audio buffers, size = capacity (always)
42         if (capacity > 0) {
43 #ifdef NO_POSIX_MEMALIGN
44                 _data =  (Sample *) malloc(sizeof(Sample) * capacity);
45 #else
46                 posix_memalign((void**)&_data, 16, sizeof(Sample) * capacity);
47 #endif  
48                 assert(_data);
49                 clear();
50                 _owns_data = true;
51         } else {
52                 _owns_data = false;
53         }
54 }
55
56 AudioBuffer::~AudioBuffer()
57 {
58         if (_owns_data)
59                 free(_data);
60 }
61
62 // FIXME: mirroring for MIDI buffers?
63 MidiBuffer::MidiBuffer(size_t capacity)
64         : Buffer(DataType::MIDI, capacity)
65         , _owns_data(true)
66         , _data(NULL)
67 {
68         assert(capacity > 0);
69
70         _size = capacity; // For audio buffers, size = capacity (always)
71 #ifdef NO_POSIX_MEMALIGN
72         _data =  (RawMidi *) malloc(sizeof(RawMidi) * capacity);
73 #else
74         posix_memalign((void**)&_data, 16, sizeof(RawMidi) * capacity);
75 #endif  
76         assert(_data);
77         memset(_data, 0, sizeof(RawMidi) * capacity);
78 }
79
80 MidiBuffer::~MidiBuffer()
81 {
82         if (_owns_data)
83                 free(_data);
84 }
85
86 /** Note that offset and nframes refer to sample time, not actual buffer locations */
87 void
88 MidiBuffer::write(const Buffer& src, jack_nframes_t offset, jack_nframes_t nframes)
89 {
90         assert(src.type() == DataType::MIDI);
91         assert(offset == 0);
92         MidiBuffer& msrc = (MidiBuffer&)src;
93         _size = 0;
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];
97                 ++_size;
98         }
99         assert(_size == msrc.size());
100
101         if (_size > 0)
102                 std::cerr << "MidiBuffer wrote " << _size << " events.\n";
103 }
104
105
106 } // namespace ARDOUR
107