Transistor Gijutsu, October 2014, Special Features Chapter 9, Software of the Function Generator トランジスタ技術2014年10月号 特集第9章のソフトウェア わがまま波形発生器のソフトウェア
Information
tg_201410s8_AD7714 トランジスタ技術 2014年 10月号 第9章のソフトウェア
Program for Section 9 in October. 2014 issue of the Transistor Gijutsu
(Japanese electronics magazine)
概要
このプログラムは、ソフトウエアDDSにより、任意の波形を出力(2ch)します。 特徴は次のとおりです。
- PWM出力をDAコンバータとして利用します。
- 周波数や波形、バースト条件などを個別に設定できる独立した出力を2チャネル持っています。
- 周波数分解能0.023mHz
- 周波数範囲0.023mHz~10kHz
- 各チャネルにそれぞれ、波形の先頭で出力されるトリガ出力があります。
- 出力波形を関数で定義できます。
- 休止波数、出力波数、を設定することでバースト波形が出力できます。
ファイル
このソフトウエアは、次のファイルから構成されています。
- DDS.cpp - DDSによる波形発生
- main.cpp - main()関数
詳細については、10月号の記事および上記ファイル中のコメントを参照してください。
USBDevice/USBMIDI/MIDIMessage.h@0:f1ecca559ec3, 2014-08-29 (annotated)
- Committer:
- Dance
- Date:
- Fri Aug 29 08:33:17 2014 +0000
- Revision:
- 0:f1ecca559ec3
Transistor Gijutsu, October 2014, Special Features Chapter 9; ????????2014?10??????9????????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Dance | 0:f1ecca559ec3 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
Dance | 0:f1ecca559ec3 | 2 | * |
Dance | 0:f1ecca559ec3 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
Dance | 0:f1ecca559ec3 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
Dance | 0:f1ecca559ec3 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
Dance | 0:f1ecca559ec3 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
Dance | 0:f1ecca559ec3 | 7 | * Software is furnished to do so, subject to the following conditions: |
Dance | 0:f1ecca559ec3 | 8 | * |
Dance | 0:f1ecca559ec3 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
Dance | 0:f1ecca559ec3 | 10 | * substantial portions of the Software. |
Dance | 0:f1ecca559ec3 | 11 | * |
Dance | 0:f1ecca559ec3 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
Dance | 0:f1ecca559ec3 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
Dance | 0:f1ecca559ec3 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
Dance | 0:f1ecca559ec3 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Dance | 0:f1ecca559ec3 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Dance | 0:f1ecca559ec3 | 17 | */ |
Dance | 0:f1ecca559ec3 | 18 | |
Dance | 0:f1ecca559ec3 | 19 | #ifndef MIDIMESSAGE_H |
Dance | 0:f1ecca559ec3 | 20 | #define MIDIMESSAGE_H |
Dance | 0:f1ecca559ec3 | 21 | |
Dance | 0:f1ecca559ec3 | 22 | #include "mbed.h" |
Dance | 0:f1ecca559ec3 | 23 | |
Dance | 0:f1ecca559ec3 | 24 | // MIDI Message Format |
Dance | 0:f1ecca559ec3 | 25 | // |
Dance | 0:f1ecca559ec3 | 26 | // [ msg(4) | channel(4) ] [ 0 | n(7) ] [ 0 | m(7) ] |
Dance | 0:f1ecca559ec3 | 27 | // |
Dance | 0:f1ecca559ec3 | 28 | // MIDI Data Messages (Channel Specific) |
Dance | 0:f1ecca559ec3 | 29 | // |
Dance | 0:f1ecca559ec3 | 30 | // Message msg n m |
Dance | 0:f1ecca559ec3 | 31 | // --------------------------------------------- |
Dance | 0:f1ecca559ec3 | 32 | // Note Off 0x8 Key Velocity |
Dance | 0:f1ecca559ec3 | 33 | // Note On 0x9 Key Velocity |
Dance | 0:f1ecca559ec3 | 34 | // Polyphonic Aftertouch 0xA Key Pressure |
Dance | 0:f1ecca559ec3 | 35 | // Control Change 0xB Controller Value |
Dance | 0:f1ecca559ec3 | 36 | // Program Change 0xC Program - |
Dance | 0:f1ecca559ec3 | 37 | // Channel Aftertouch 0xD Pressure - |
Dance | 0:f1ecca559ec3 | 38 | // Pitch Wheel 0xE LSB MSB |
Dance | 0:f1ecca559ec3 | 39 | |
Dance | 0:f1ecca559ec3 | 40 | #define CABLE_NUM (0<<4) |
Dance | 0:f1ecca559ec3 | 41 | |
Dance | 0:f1ecca559ec3 | 42 | /** A MIDI message container */ |
Dance | 0:f1ecca559ec3 | 43 | class MIDIMessage { |
Dance | 0:f1ecca559ec3 | 44 | public: |
Dance | 0:f1ecca559ec3 | 45 | MIDIMessage() {} |
Dance | 0:f1ecca559ec3 | 46 | |
Dance | 0:f1ecca559ec3 | 47 | MIDIMessage(uint8_t *buf) { |
Dance | 0:f1ecca559ec3 | 48 | for (int i = 0; i < 4; i++) |
Dance | 0:f1ecca559ec3 | 49 | data[i] = buf[i]; |
Dance | 0:f1ecca559ec3 | 50 | } |
Dance | 0:f1ecca559ec3 | 51 | |
Dance | 0:f1ecca559ec3 | 52 | // create messages |
Dance | 0:f1ecca559ec3 | 53 | |
Dance | 0:f1ecca559ec3 | 54 | /** Create a NoteOff message |
Dance | 0:f1ecca559ec3 | 55 | * @param key Key ID |
Dance | 0:f1ecca559ec3 | 56 | * @param velocity Key velocity (0-127, default = 127) |
Dance | 0:f1ecca559ec3 | 57 | * @param channel Key channel (0-15, default 0) |
Dance | 0:f1ecca559ec3 | 58 | * @returns A MIDIMessage |
Dance | 0:f1ecca559ec3 | 59 | */ |
Dance | 0:f1ecca559ec3 | 60 | static MIDIMessage NoteOff(int key, int velocity = 127, int channel = 0) { |
Dance | 0:f1ecca559ec3 | 61 | MIDIMessage msg; |
Dance | 0:f1ecca559ec3 | 62 | msg.data[0] = CABLE_NUM | 0x08; |
Dance | 0:f1ecca559ec3 | 63 | msg.data[1] = 0x80 | (channel & 0x0F); |
Dance | 0:f1ecca559ec3 | 64 | msg.data[2] = key & 0x7F; |
Dance | 0:f1ecca559ec3 | 65 | msg.data[3] = velocity & 0x7F; |
Dance | 0:f1ecca559ec3 | 66 | return msg; |
Dance | 0:f1ecca559ec3 | 67 | } |
Dance | 0:f1ecca559ec3 | 68 | |
Dance | 0:f1ecca559ec3 | 69 | /** Create a NoteOn message |
Dance | 0:f1ecca559ec3 | 70 | * @param key Key ID |
Dance | 0:f1ecca559ec3 | 71 | * @param velocity Key velocity (0-127, default = 127) |
Dance | 0:f1ecca559ec3 | 72 | * @param channel Key channel (0-15, default 0) |
Dance | 0:f1ecca559ec3 | 73 | * @returns A MIDIMessage |
Dance | 0:f1ecca559ec3 | 74 | */ |
Dance | 0:f1ecca559ec3 | 75 | static MIDIMessage NoteOn(int key, int velocity = 127, int channel = 0) { |
Dance | 0:f1ecca559ec3 | 76 | MIDIMessage msg; |
Dance | 0:f1ecca559ec3 | 77 | msg.data[0] = CABLE_NUM | 0x09; |
Dance | 0:f1ecca559ec3 | 78 | msg.data[1] = 0x90 | (channel & 0x0F); |
Dance | 0:f1ecca559ec3 | 79 | msg.data[2] = key & 0x7F; |
Dance | 0:f1ecca559ec3 | 80 | msg.data[3] = velocity & 0x7F; |
Dance | 0:f1ecca559ec3 | 81 | return msg; |
Dance | 0:f1ecca559ec3 | 82 | } |
Dance | 0:f1ecca559ec3 | 83 | |
Dance | 0:f1ecca559ec3 | 84 | /** Create a PolyPhonic Aftertouch message |
Dance | 0:f1ecca559ec3 | 85 | * @param key Key ID |
Dance | 0:f1ecca559ec3 | 86 | * @param pressure Aftertouch pressure (0-127) |
Dance | 0:f1ecca559ec3 | 87 | * @param channel Key channel (0-15, default 0) |
Dance | 0:f1ecca559ec3 | 88 | * @returns A MIDIMessage |
Dance | 0:f1ecca559ec3 | 89 | */ |
Dance | 0:f1ecca559ec3 | 90 | static MIDIMessage PolyphonicAftertouch(int key, int pressure, int channel = 0) { |
Dance | 0:f1ecca559ec3 | 91 | MIDIMessage msg; |
Dance | 0:f1ecca559ec3 | 92 | msg.data[0] = CABLE_NUM | 0x0A; |
Dance | 0:f1ecca559ec3 | 93 | msg.data[1] = 0xA0 | (channel & 0x0F); |
Dance | 0:f1ecca559ec3 | 94 | msg.data[2] = key & 0x7F; |
Dance | 0:f1ecca559ec3 | 95 | msg.data[3] = pressure & 0x7F; |
Dance | 0:f1ecca559ec3 | 96 | return msg; |
Dance | 0:f1ecca559ec3 | 97 | } |
Dance | 0:f1ecca559ec3 | 98 | |
Dance | 0:f1ecca559ec3 | 99 | /** Create a Control Change message |
Dance | 0:f1ecca559ec3 | 100 | * @param control Controller ID |
Dance | 0:f1ecca559ec3 | 101 | * @param value Controller value (0-127) |
Dance | 0:f1ecca559ec3 | 102 | * @param channel Controller channel (0-15, default 0) |
Dance | 0:f1ecca559ec3 | 103 | * @returns A MIDIMessage |
Dance | 0:f1ecca559ec3 | 104 | */ |
Dance | 0:f1ecca559ec3 | 105 | static MIDIMessage ControlChange(int control, int value, int channel = 0) { |
Dance | 0:f1ecca559ec3 | 106 | MIDIMessage msg; |
Dance | 0:f1ecca559ec3 | 107 | msg.data[0] = CABLE_NUM | 0x0B; |
Dance | 0:f1ecca559ec3 | 108 | msg.data[1] = 0xB0 | (channel & 0x0F); |
Dance | 0:f1ecca559ec3 | 109 | msg.data[2] = control & 0x7F; |
Dance | 0:f1ecca559ec3 | 110 | msg.data[3] = value & 0x7F; |
Dance | 0:f1ecca559ec3 | 111 | return msg; |
Dance | 0:f1ecca559ec3 | 112 | } |
Dance | 0:f1ecca559ec3 | 113 | |
Dance | 0:f1ecca559ec3 | 114 | /** Create a Program Change message |
Dance | 0:f1ecca559ec3 | 115 | * @param program Program ID |
Dance | 0:f1ecca559ec3 | 116 | * @param channel Channel (0-15, default 0) |
Dance | 0:f1ecca559ec3 | 117 | * @returns A MIDIMessage |
Dance | 0:f1ecca559ec3 | 118 | */ |
Dance | 0:f1ecca559ec3 | 119 | static MIDIMessage ProgramChange(int program, int channel = 0) { |
Dance | 0:f1ecca559ec3 | 120 | MIDIMessage msg; |
Dance | 0:f1ecca559ec3 | 121 | msg.data[0] = CABLE_NUM | 0x0C; |
Dance | 0:f1ecca559ec3 | 122 | msg.data[1] = 0xC0 | (channel & 0x0F); |
Dance | 0:f1ecca559ec3 | 123 | msg.data[2] = program & 0x7F; |
Dance | 0:f1ecca559ec3 | 124 | msg.data[3] = 0x00; |
Dance | 0:f1ecca559ec3 | 125 | return msg; |
Dance | 0:f1ecca559ec3 | 126 | } |
Dance | 0:f1ecca559ec3 | 127 | |
Dance | 0:f1ecca559ec3 | 128 | /** Create a Channel Aftertouch message |
Dance | 0:f1ecca559ec3 | 129 | * @param pressure Pressure |
Dance | 0:f1ecca559ec3 | 130 | * @param channel Key channel (0-15, default 0) |
Dance | 0:f1ecca559ec3 | 131 | * @returns A MIDIMessage |
Dance | 0:f1ecca559ec3 | 132 | */ |
Dance | 0:f1ecca559ec3 | 133 | static MIDIMessage ChannelAftertouch(int pressure, int channel = 0) { |
Dance | 0:f1ecca559ec3 | 134 | MIDIMessage msg; |
Dance | 0:f1ecca559ec3 | 135 | msg.data[0] = CABLE_NUM | 0x0D; |
Dance | 0:f1ecca559ec3 | 136 | msg.data[1] = 0xD0 | (channel & 0x0F); |
Dance | 0:f1ecca559ec3 | 137 | msg.data[2] = pressure & 0x7F; |
Dance | 0:f1ecca559ec3 | 138 | msg.data[3] = 0x00; |
Dance | 0:f1ecca559ec3 | 139 | return msg; |
Dance | 0:f1ecca559ec3 | 140 | } |
Dance | 0:f1ecca559ec3 | 141 | |
Dance | 0:f1ecca559ec3 | 142 | /** Create a Pitch Wheel message |
Dance | 0:f1ecca559ec3 | 143 | * @param pitch Pitch (-8192 - 8191, default = 0) |
Dance | 0:f1ecca559ec3 | 144 | * @param channel Channel (0-15, default 0) |
Dance | 0:f1ecca559ec3 | 145 | * @returns A MIDIMessage |
Dance | 0:f1ecca559ec3 | 146 | */ |
Dance | 0:f1ecca559ec3 | 147 | static MIDIMessage PitchWheel(int pitch = 0, int channel = 0) { |
Dance | 0:f1ecca559ec3 | 148 | MIDIMessage msg; |
Dance | 0:f1ecca559ec3 | 149 | int p = pitch + 8192; // 0 - 16383, 8192 is center |
Dance | 0:f1ecca559ec3 | 150 | msg.data[0] = CABLE_NUM | 0x0E; |
Dance | 0:f1ecca559ec3 | 151 | msg.data[1] = 0xE0 | (channel & 0x0F); |
Dance | 0:f1ecca559ec3 | 152 | msg.data[2] = p & 0x7F; |
Dance | 0:f1ecca559ec3 | 153 | msg.data[3] = (p >> 7) & 0x7F; |
Dance | 0:f1ecca559ec3 | 154 | return msg; |
Dance | 0:f1ecca559ec3 | 155 | } |
Dance | 0:f1ecca559ec3 | 156 | |
Dance | 0:f1ecca559ec3 | 157 | /** Create an All Notes Off message |
Dance | 0:f1ecca559ec3 | 158 | * @param channel Channel (0-15, default 0) |
Dance | 0:f1ecca559ec3 | 159 | * @returns A MIDIMessage |
Dance | 0:f1ecca559ec3 | 160 | */ |
Dance | 0:f1ecca559ec3 | 161 | static MIDIMessage AllNotesOff(int channel = 0) { |
Dance | 0:f1ecca559ec3 | 162 | return ControlChange(123, 0, channel); |
Dance | 0:f1ecca559ec3 | 163 | } |
Dance | 0:f1ecca559ec3 | 164 | |
Dance | 0:f1ecca559ec3 | 165 | // decode messages |
Dance | 0:f1ecca559ec3 | 166 | |
Dance | 0:f1ecca559ec3 | 167 | /** MIDI Message Types */ |
Dance | 0:f1ecca559ec3 | 168 | enum MIDIMessageType { |
Dance | 0:f1ecca559ec3 | 169 | ErrorType, |
Dance | 0:f1ecca559ec3 | 170 | NoteOffType, |
Dance | 0:f1ecca559ec3 | 171 | NoteOnType, |
Dance | 0:f1ecca559ec3 | 172 | PolyphonicAftertouchType, |
Dance | 0:f1ecca559ec3 | 173 | ControlChangeType, |
Dance | 0:f1ecca559ec3 | 174 | ProgramChangeType, |
Dance | 0:f1ecca559ec3 | 175 | ChannelAftertouchType, |
Dance | 0:f1ecca559ec3 | 176 | PitchWheelType, |
Dance | 0:f1ecca559ec3 | 177 | AllNotesOffType |
Dance | 0:f1ecca559ec3 | 178 | }; |
Dance | 0:f1ecca559ec3 | 179 | |
Dance | 0:f1ecca559ec3 | 180 | /** Read the message type |
Dance | 0:f1ecca559ec3 | 181 | * @returns MIDIMessageType |
Dance | 0:f1ecca559ec3 | 182 | */ |
Dance | 0:f1ecca559ec3 | 183 | MIDIMessageType type() { |
Dance | 0:f1ecca559ec3 | 184 | switch((data[1] >> 4) & 0xF) { |
Dance | 0:f1ecca559ec3 | 185 | case 0x8: return NoteOffType; |
Dance | 0:f1ecca559ec3 | 186 | case 0x9: return NoteOnType; |
Dance | 0:f1ecca559ec3 | 187 | case 0xA: return PolyphonicAftertouchType; |
Dance | 0:f1ecca559ec3 | 188 | case 0xB: |
Dance | 0:f1ecca559ec3 | 189 | if(controller() < 120) { // standard controllers |
Dance | 0:f1ecca559ec3 | 190 | return ControlChangeType; |
Dance | 0:f1ecca559ec3 | 191 | } else if(controller() == 123) { |
Dance | 0:f1ecca559ec3 | 192 | return AllNotesOffType; |
Dance | 0:f1ecca559ec3 | 193 | } else { |
Dance | 0:f1ecca559ec3 | 194 | return ErrorType; // unsupported atm |
Dance | 0:f1ecca559ec3 | 195 | } |
Dance | 0:f1ecca559ec3 | 196 | case 0xC: return ProgramChangeType; |
Dance | 0:f1ecca559ec3 | 197 | case 0xD: return ChannelAftertouchType; |
Dance | 0:f1ecca559ec3 | 198 | case 0xE: return PitchWheelType; |
Dance | 0:f1ecca559ec3 | 199 | default: return ErrorType; |
Dance | 0:f1ecca559ec3 | 200 | } |
Dance | 0:f1ecca559ec3 | 201 | } |
Dance | 0:f1ecca559ec3 | 202 | |
Dance | 0:f1ecca559ec3 | 203 | /** Read the channel number */ |
Dance | 0:f1ecca559ec3 | 204 | int channel() { |
Dance | 0:f1ecca559ec3 | 205 | return (data[1] & 0x0F); |
Dance | 0:f1ecca559ec3 | 206 | } |
Dance | 0:f1ecca559ec3 | 207 | |
Dance | 0:f1ecca559ec3 | 208 | /** Read the key ID */ |
Dance | 0:f1ecca559ec3 | 209 | int key() { |
Dance | 0:f1ecca559ec3 | 210 | return (data[2] & 0x7F); |
Dance | 0:f1ecca559ec3 | 211 | } |
Dance | 0:f1ecca559ec3 | 212 | |
Dance | 0:f1ecca559ec3 | 213 | /** Read the velocity */ |
Dance | 0:f1ecca559ec3 | 214 | int velocity() { |
Dance | 0:f1ecca559ec3 | 215 | return (data[3] & 0x7F); |
Dance | 0:f1ecca559ec3 | 216 | } |
Dance | 0:f1ecca559ec3 | 217 | |
Dance | 0:f1ecca559ec3 | 218 | /** Read the controller value */ |
Dance | 0:f1ecca559ec3 | 219 | int value() { |
Dance | 0:f1ecca559ec3 | 220 | return (data[3] & 0x7F); |
Dance | 0:f1ecca559ec3 | 221 | } |
Dance | 0:f1ecca559ec3 | 222 | |
Dance | 0:f1ecca559ec3 | 223 | /** Read the aftertouch pressure */ |
Dance | 0:f1ecca559ec3 | 224 | int pressure() { |
Dance | 0:f1ecca559ec3 | 225 | if(type() == PolyphonicAftertouchType) { |
Dance | 0:f1ecca559ec3 | 226 | return (data[3] & 0x7F); |
Dance | 0:f1ecca559ec3 | 227 | } else { |
Dance | 0:f1ecca559ec3 | 228 | return (data[2] & 0x7F); |
Dance | 0:f1ecca559ec3 | 229 | } |
Dance | 0:f1ecca559ec3 | 230 | } |
Dance | 0:f1ecca559ec3 | 231 | |
Dance | 0:f1ecca559ec3 | 232 | /** Read the controller number */ |
Dance | 0:f1ecca559ec3 | 233 | int controller() { |
Dance | 0:f1ecca559ec3 | 234 | return (data[2] & 0x7F); |
Dance | 0:f1ecca559ec3 | 235 | } |
Dance | 0:f1ecca559ec3 | 236 | |
Dance | 0:f1ecca559ec3 | 237 | /** Read the program number */ |
Dance | 0:f1ecca559ec3 | 238 | int program() { |
Dance | 0:f1ecca559ec3 | 239 | return (data[2] & 0x7F); |
Dance | 0:f1ecca559ec3 | 240 | } |
Dance | 0:f1ecca559ec3 | 241 | |
Dance | 0:f1ecca559ec3 | 242 | /** Read the pitch value */ |
Dance | 0:f1ecca559ec3 | 243 | int pitch() { |
Dance | 0:f1ecca559ec3 | 244 | int p = ((data[3] & 0x7F) << 7) | (data[2] & 0x7F); |
Dance | 0:f1ecca559ec3 | 245 | return p - 8192; // 0 - 16383, 8192 is center |
Dance | 0:f1ecca559ec3 | 246 | } |
Dance | 0:f1ecca559ec3 | 247 | |
Dance | 0:f1ecca559ec3 | 248 | uint8_t data[4]; |
Dance | 0:f1ecca559ec3 | 249 | }; |
Dance | 0:f1ecca559ec3 | 250 | |
Dance | 0:f1ecca559ec3 | 251 | #endif |