Jamoma API  0.6.0.a19
TTMidiFilter.cpp
1 /*
2  * Object to filter common types of MIDI events out of raw MIDI event dictionaries
3  * Copyright © 2011, Timothy Place
4  *
5  * License: This code is licensed under the terms of the "New BSD License"
6  * http://creativecommons.org/licenses/BSD/
7  */
8 
9 #include "TTGraphObjectBase.h"
10 #include "TTMidiFilter.h"
11 #ifdef TT_PLATFORM_WIN
12 #include <algorithm>
13 #endif
14 
15 #define thisTTClass TTMidiFilter
16 #define thisTTClassName "midi.filter"
17 #define thisTTClassTags "midi"
18 
19 
21  mType(kTTSymEmpty)
22 {
23  addAttribute(Type, kTypeSymbol);
24  addMessageWithArguments(dictionary);
25 
26  setAttributeValue(TT("type"), TT("note"));
27 }
28 
29 
30 TTMidiFilter::~TTMidiFilter()
31 {
32  ;
33 }
34 
35 
36 TTErr TTMidiFilter::dictionary(const TTValue& input, TTValue& output)
37 {
38  TTDictionaryPtr d = NULL;
39  TTSymbol schema;
40 
41  //input.get(0, (TTPtr*)(&d));
42  d = TTDictionaryPtr(TTPtr(input[0]));
43 
44  schema = d->getSchema();
45  if (schema == TT("RawMidiEvent")) {
46  TTValue statusByteValue;
47  TTValue dataByte1Value;
48  TTValue dataByte2Value;
49  TTUInt8 statusByte;
50 
51  d->lookup(TT("status"), statusByteValue);
52  d->lookup(TT("data1"), dataByte1Value);
53  d->lookup(TT("data2"), dataByte2Value);
54  statusByte = statusByteValue[0];
55 
56  if (mType == TT("note")) {
57  bool noteon = false;
58  bool noteoff = false;
59 
60  if (statusByte > 127 && statusByte < 144)
61  noteoff = true;
62  else if (statusByte > 143 && statusByte < 160)
63  noteon = true;
64  else
65  goto out;
66 
67  {
68  TTValue v;
69 
70  v.resize(3);
71  v[0] = TTUInt8(dataByte1Value);
72  if (noteon) {
73  v[1] = TTUInt8(dataByte2Value);
74  v[2] = statusByte - 143;
75  }
76  else {
77  v[1] = TTUInt8(0);
78  v[2] = statusByte - 127;
79  }
80 
81  d->setSchema(TT("MidiNoteEvent"));
82  d->setValue(v);
83  return kTTErrNone;
84  }
85  }
86  out:
87  d->clear();
88  output[0] = TTPtr(d);
89  return kTTErrNone;
90  }
91  else {
92  return kTTErrInvalidType;
93  }
94 
95 }
96 
#define addAttribute(name, type)
A convenience macro to be used by subclasses for registering attributes with a custom getter...
Definition: TTAttribute.h:29
TTErr clear()
Remove all items from the hash table.
Definition: TTDictionary.h:240
Bad DataType for the context.
Definition: TTBase.h:347
Jamoma Asynchronous Object Graph Layer.
Symbol type.
Definition: TTBase.h:282
A type that represents the key as a C-String and the value as a pointer to the matching TTSymbol obje...
Definition: TTDictionary.h:47
#define TT
This macro is defined as a shortcut for doing a lookup in the symbol table.
Definition: TTSymbol.h:155
void * TTPtr
A generic pointer.
Definition: TTBase.h:248
The TTSymbol class is used to represent a string and efficiently pass and compare that string...
Definition: TTSymbol.h:26
const TTSymbol getSchema() const
TODO: Add documentation.
Definition: TTDictionary.h:181
#define addMessageWithArguments(name)
A convenience macro to be used by subclasses for registering messages.
Definition: TTMessage.h:27
TTErr setSchema(const TTSymbol aSchemaName)
TODO: Add documentation schemaName TODO: Add documentation.
Definition: TTDictionary.h:172
TTErr setValue(const TTValue aNewValue)
TODO: Add documentation.
Definition: TTDictionary.h:191
TTErr
Jamoma Error Codes Enumeration of error codes that might be returned by any of the TTBlue functions a...
Definition: TTBase.h:342
TTErr lookup(const TTSymbol aKey, TTValue &aValue) const
Find the value for the given key.
Definition: TTDictionary.h:221
No Error.
Definition: TTBase.h:343
TTDictionary * TTDictionaryPtr
[doxygenAppendixC_typedefExample]
Definition: TTDictionary.h:279
TT_OBJECT_CONSTRUCTOR
Constructor macro.
void resize(size_type n)
Change the number of elements.
[doxygenAppendixC_copyExample]
Definition: TTValue.h:34
unsigned char TTUInt8
8 bit unsigned integer (char)
Definition: TTBase.h:174