3 * Copyright (C) 2012-2019 Filipe Coelho <falktx@falktx.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * For a full copy of the GNU General Public License see the doc/GPL.txt file.
18 #include "CarlaNative.h"
19 #include "CarlaMIDI.h"
23 // -----------------------------------------------------------------------
29 } MidiTransposeParams
;
32 const NativeHostDescriptor
* host
;
35 } MidiTransposeHandle
;
37 // -----------------------------------------------------------------------
39 static NativePluginHandle
miditranspose_instantiate(const NativeHostDescriptor
* host
)
41 MidiTransposeHandle
* const handle
= (MidiTransposeHandle
*)malloc(sizeof(MidiTransposeHandle
));
48 handle
->semitones
= 0;
52 #define handlePtr ((MidiTransposeHandle*)handle)
54 static void miditranspose_cleanup(NativePluginHandle handle
)
59 static uint32_t miditranspose_get_parameter_count(NativePluginHandle handle
)
67 static const NativeParameter
* miditranspose_get_parameter_info(NativePluginHandle handle
, uint32_t index
)
69 if (index
> PARAM_COUNT
)
72 static NativeParameter param
;
74 param
.hints
= NATIVE_PARAMETER_IS_ENABLED
|NATIVE_PARAMETER_IS_AUTOMATABLE
|NATIVE_PARAMETER_IS_INTEGER
;
76 param
.scalePointCount
= 0;
77 param
.scalePoints
= NULL
;
82 param
.name
= "Octaves";
83 param
.ranges
.def
= 0.0f
,
84 param
.ranges
.min
= -8.0f
,
85 param
.ranges
.max
= 8.0f
,
86 param
.ranges
.step
= 1.0f
;
87 param
.ranges
.stepSmall
= 1.0f
;
88 param
.ranges
.stepLarge
= 4.0f
;
91 param
.name
= "Semitones";
92 param
.ranges
.def
= 0.0f
,
93 param
.ranges
.min
= -12.0f
,
94 param
.ranges
.max
= 12.0f
,
95 param
.ranges
.step
= 1.0f
;
96 param
.ranges
.stepSmall
= 1.0f
;
97 param
.ranges
.stepLarge
= 4.0f
;
107 static float miditranspose_get_parameter_value(NativePluginHandle handle
, uint32_t index
)
112 return (float)handlePtr
->octaves
;
113 case PARAM_SEMITONES
:
114 return (float)handlePtr
->semitones
;
120 static void miditranspose_set_parameter_value(NativePluginHandle handle
, uint32_t index
, float value
)
125 handlePtr
->octaves
= (int)value
;
127 case PARAM_SEMITONES
:
128 handlePtr
->semitones
= (int)value
;
133 // FIXME for v3.0, use const for the input buffer
134 static void miditranspose_process(NativePluginHandle handle
,
135 float** inBuffer
, float** outBuffer
, uint32_t frames
,
136 const NativeMidiEvent
* midiEvents
, uint32_t midiEventCount
)
138 const NativeHostDescriptor
* const host
= handlePtr
->host
;
139 const int octaves
= handlePtr
->octaves
;
140 const int semitones
= handlePtr
->semitones
;
141 NativeMidiEvent tmpEvent
;
143 for (uint32_t i
=0; i
< midiEventCount
; ++i
)
145 const NativeMidiEvent
* const midiEvent
= &midiEvents
[i
];
147 const uint8_t status
= (uint8_t)MIDI_GET_STATUS_FROM_DATA(midiEvent
->data
);
149 if (status
== MIDI_STATUS_NOTE_OFF
|| status
== MIDI_STATUS_NOTE_ON
)
151 int oldnote
= midiEvent
->data
[1];
152 int newnote
= oldnote
+ octaves
*12 + semitones
;
154 if (newnote
< 0 || newnote
>= MAX_MIDI_NOTE
)
157 tmpEvent
.port
= midiEvent
->port
;
158 tmpEvent
.time
= midiEvent
->time
;
159 tmpEvent
.data
[0] = midiEvent
->data
[0];
160 tmpEvent
.data
[1] = (uint8_t)newnote
;
161 tmpEvent
.data
[2] = midiEvent
->data
[2];
162 tmpEvent
.data
[3] = midiEvent
->data
[3];
163 tmpEvent
.size
= midiEvent
->size
;
165 host
->write_midi_event(host
->handle
, &tmpEvent
);
168 host
->write_midi_event(host
->handle
, midiEvent
);
181 // -----------------------------------------------------------------------
183 static const NativePluginDescriptor miditransposeDesc
= {
184 .category
= NATIVE_PLUGIN_CATEGORY_UTILITY
,
185 .hints
= NATIVE_PLUGIN_IS_RTSAFE
,
186 .supports
= NATIVE_PLUGIN_SUPPORTS_EVERYTHING
,
195 .name
= "MIDI Transpose",
196 .label
= "miditranspose",
198 .copyright
= "GNU GPL v2+",
200 .instantiate
= miditranspose_instantiate
,
201 .cleanup
= miditranspose_cleanup
,
203 .get_parameter_count
= miditranspose_get_parameter_count
,
204 .get_parameter_info
= miditranspose_get_parameter_info
,
205 .get_parameter_value
= miditranspose_get_parameter_value
,
207 .get_midi_program_count
= NULL
,
208 .get_midi_program_info
= NULL
,
210 .set_parameter_value
= miditranspose_set_parameter_value
,
211 .set_midi_program
= NULL
,
212 .set_custom_data
= NULL
,
217 .ui_set_parameter_value
= NULL
,
218 .ui_set_midi_program
= NULL
,
219 .ui_set_custom_data
= NULL
,
223 .process
= miditranspose_process
,
231 // -----------------------------------------------------------------------
233 void carla_register_native_plugin_miditranspose(void);
235 void carla_register_native_plugin_miditranspose(void)
237 carla_register_native_plugin(&miditransposeDesc
);
240 // -----------------------------------------------------------------------