1 : <?php
2 :
3 : /**
4 : * \Midi\Event\MetaEvent
5 : *
6 : * @package Midi
7 : * @subpackage Event
8 : * @copyright © 2009 Tommy Montgomery <http://phpmidiparser.com/>
9 : * @since 1.0
10 : */
11 :
12 : namespace Midi\Event;
13 :
14 : use \Midi\Event;
15 : use \Midi\Util\Util;
16 :
17 : /**
18 : * Base class for all meta events
19 : *
20 : * @package Midi
21 : * @subpackage Event
22 : * @since 1.0
23 : * @todo Still missing meta events 0xF1-0xFE
24 : */
25 1 : abstract class MetaEvent implements Parameterized, Event {
26 :
27 : /**
28 : * The length in bytes of the data
29 : *
30 : * @var mixed
31 : */
32 : protected $length;
33 :
34 : /**
35 : * The content of the meta event
36 : *
37 : * @var string|array
38 : */
39 : protected $data;
40 :
41 : /**
42 : * Constructor
43 : *
44 : * @since 1.0
45 : *
46 : * @param string|array $data
47 : */
48 : public function __construct($data = null) {
49 42 : if (is_string($data)) {
50 23 : $this->length = strlen($data);
51 42 : } else if (is_array($data)) {
52 18 : $this->length = count($data);
53 18 : } else {
54 4 : $this->length = 0;
55 : }
56 :
57 42 : $this->data = $data;
58 42 : }
59 :
60 : /**
61 : * Gets the length of this meta event in bytes
62 : *
63 : * @since 1.0
64 : * @uses Util::getDeltaByteSequence()
65 : *
66 : * @return int
67 : */
68 : public function getLength() {
69 1 : $lengthOfDelta = strlen(Util::getDeltaByteSequence($this->length));
70 :
71 : //meta event (0xFF), meta event type, length of data, data
72 1 : return 1 + 1 + $lengthOfDelta + $this->length;
73 : }
74 :
75 : /**
76 : * Gets the string representation of this meta event
77 : *
78 : * @since 1.0
79 : * @uses EventType::getEventName()
80 : * @uses MetaEventType::getEventTypeName()
81 : * @uses getParamDescription()
82 : * @uses getType()
83 : * @uses getSubtype()
84 : *
85 : * @return string
86 : */
87 : public function __toString() {
88 1 : return MetaEventType::getEventTypeName($this->getSubtype()) . ': ' . $this->getParamDescription();
89 : }
90 :
91 : /**
92 : * Gets the binary representation of this meta event
93 : *
94 : * @since 1.0
95 : * @uses Util::pack()
96 : * @uses Util::getDeltaByteSequence()
97 : * @uses getSubtype()
98 : *
99 : * @return binary
100 : */
101 : public function toBinary() {
102 2 : if (is_array($this->data)) {
103 1 : $data = '';
104 1 : foreach ($this->data as $datum) {
105 1 : $data .= Util::pack($datum);
106 1 : }
107 1 : } else {
108 1 : $data = $this->data;
109 : }
110 :
111 : return
112 2 : Util::pack($this->getType(), $this->getSubtype()) .
113 2 : Util::getDeltaByteSequence($this->length) .
114 2 : $data;
115 : }
116 :
117 : /**
118 : * Gets the data associated with this meta event
119 : *
120 : * @since 1.0
121 : * @uses getSubtype()
122 : *
123 : * @return array [0] => meta subtype, [1] => length, [2] => data
124 : */
125 : public function getData() {
126 : return array(
127 1 : $this->getSubtype(),
128 1 : $this->length,
129 1 : $this->data
130 1 : );
131 : }
132 :
133 : /**
134 : * Gets the event type
135 : *
136 : * @since 1.0
137 : * @uses EventType::META
138 : *
139 : * @return int
140 : */
141 : public function getType() {
142 3 : return EventType::META;
143 : }
144 :
145 : /**
146 : * Gets the meta event subtype for this event
147 : *
148 : * @since 1.0
149 : *
150 : * @return int
151 : */
152 : public abstract function getSubtype();
153 :
154 : }
155 :
|