~ubuntu-branches/ubuntu/utopic/ardour3/utopic

« back to all changes in this revision

Viewing changes to libs/evoral/evoral/EventRingBuffer.hpp

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2013-09-21 19:05:02 UTC
  • Revision ID: package-import@ubuntu.com-20130921190502-8gsftrku6jnzhd7v
Tags: upstream-3.4~dfsg
ImportĀ upstreamĀ versionĀ 3.4~dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Evoral.
 
2
 * Copyright (C) 2008 David Robillard <http://drobilla.net>
 
3
 *
 
4
 * Evoral is free software; you can redistribute it and/or modify it under the
 
5
 * terms of the GNU General Public License as published by the Free Software
 
6
 * Foundation; either version 2 of the License, or (at your option) any later
 
7
 * version.
 
8
 *
 
9
 * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
 
10
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
11
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
15
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 */
 
17
 
 
18
#ifndef EVORAL_EVENT_RING_BUFFER_HPP
 
19
#define EVORAL_EVENT_RING_BUFFER_HPP
 
20
 
 
21
#include <iostream>
 
22
 
 
23
#include "pbd/ringbufferNPT.h"
 
24
 
 
25
#include "evoral/EventSink.hpp"
 
26
#include "evoral/types.hpp"
 
27
 
 
28
using namespace std;
 
29
 
 
30
namespace Evoral {
 
31
 
 
32
/** A RingBuffer of events (generic time-stamped binary "blobs").
 
33
 *
 
34
 * This packs a timestamp, size, and size bytes of data flat into the buffer.
 
35
 * Useful for MIDI events, OSC messages, etc.
 
36
 *
 
37
 * Note: the uint8_t template argument to RingBufferNPT<> indicates "byte
 
38
 * oriented data", not anything particular linked to MIDI or any other
 
39
 * possible interpretation of uint8_t.
 
40
 */
 
41
template<typename Time>
 
42
class EventRingBuffer : public PBD::RingBufferNPT<uint8_t>, public Evoral::EventSink<Time> {
 
43
public:
 
44
 
 
45
        /** @param capacity Ringbuffer capacity in bytes.
 
46
         */
 
47
        EventRingBuffer(size_t capacity) : PBD::RingBufferNPT<uint8_t>(capacity)
 
48
        {}
 
49
 
 
50
        inline size_t capacity() const { return bufsize(); }
 
51
 
 
52
        /** Peek at the ringbuffer (read w/o advancing read pointer).
 
53
         * @return how much has been peeked (wraps around if read exceeds
 
54
         * the end of the buffer):
 
55
         * <pre>
 
56
         * |===========--------------R=============================|
 
57
         *            read-pointer---^
 
58
         * </pre>
 
59
         */
 
60
        inline bool peek (uint8_t*, size_t size);
 
61
 
 
62
        inline uint32_t write(Time  time, EventType  type, uint32_t  size, const uint8_t* buf);
 
63
        inline bool     read (Time* time, EventType* type, uint32_t* size,       uint8_t* buf);
 
64
};
 
65
 
 
66
template<typename Time>
 
67
inline bool
 
68
EventRingBuffer<Time>::peek (uint8_t* buf, size_t size)
 
69
{
 
70
        PBD::RingBufferNPT<uint8_t>::rw_vector vec;
 
71
 
 
72
        get_read_vector (&vec);
 
73
 
 
74
        if (vec.len[0] + vec.len[1] < size) {
 
75
                return false;
 
76
        }
 
77
 
 
78
        if (vec.len[0] > 0) {
 
79
                memcpy (buf, vec.buf[0], min (vec.len[0], size));
 
80
        }
 
81
 
 
82
        if (vec.len[0] < size) {
 
83
                if (vec.len[1]) {
 
84
                        memcpy (buf + vec.len[0], vec.buf[1], size - vec.len[0]);
 
85
                }
 
86
        }
 
87
 
 
88
        return true;
 
89
}
 
90
 
 
91
template<typename Time>
 
92
inline bool
 
93
EventRingBuffer<Time>::read(Time* time, EventType* type, uint32_t* size, uint8_t* buf)
 
94
{
 
95
        if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)time, sizeof (Time)) != sizeof (Time)) {
 
96
                return false;
 
97
        }
 
98
 
 
99
        if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)type, sizeof(EventType)) != sizeof (EventType)) {
 
100
                return false;
 
101
        }
 
102
 
 
103
        if (PBD::RingBufferNPT<uint8_t>::read ((uint8_t*)size, sizeof(uint32_t)) != sizeof (uint32_t)) {
 
104
                return false;
 
105
        }
 
106
 
 
107
        if (PBD::RingBufferNPT<uint8_t>::read (buf, *size) != *size) {
 
108
                return false;
 
109
        }
 
110
 
 
111
        return true;
 
112
}
 
113
 
 
114
 
 
115
template<typename Time>
 
116
inline uint32_t
 
117
EventRingBuffer<Time>::write(Time time, EventType type, uint32_t size, const uint8_t* buf)
 
118
{
 
119
        if (!buf || write_space() < (sizeof(Time) + sizeof(EventType) + sizeof(uint32_t) + size)) {
 
120
                return 0;
 
121
        } else {
 
122
                PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&time, sizeof(Time));
 
123
                PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&type, sizeof(EventType));
 
124
                PBD::RingBufferNPT<uint8_t>::write ((uint8_t*)&size, sizeof(uint32_t));
 
125
                PBD::RingBufferNPT<uint8_t>::write (buf, size);
 
126
                return size;
 
127
        }
 
128
}
 
129
 
 
130
 
 
131
} // namespace Evoral
 
132
 
 
133
#endif // EVORAL_EVENT_RING_BUFFER_HPP
 
134