Nucleo F401REでFM音源を実装するやつ の実装途中で32ポリ音源にしたやつ 外部DACとオペアンプを利用 現在はMCP4922とNJM2737

Dependencies:   AOTTrigon I2CEEPROM MCP4922 mbed

Files at this revision

API Documentation at this revision

Comitter:
kb10uy
Date:
Mon Dec 29 12:48:44 2014 +0000
Parent:
10:0ffdefe75566
Commit message:
32poly??????????

Changed in this revision

MIDI.cpp Show annotated file Show diff for this revision Revisions of this file
MIDI.h Show annotated file Show diff for this revision Revisions of this file
MIDIUtil.h Show annotated file Show diff for this revision Revisions of this file
Main.cpp Show annotated file Show diff for this revision Revisions of this file
Main.h Show annotated file Show diff for this revision Revisions of this file
diff -r 0ffdefe75566 -r 62da91a1eaf1 MIDI.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MIDI.cpp	Mon Dec 29 12:48:44 2014 +0000
@@ -0,0 +1,138 @@
+#include "MIDI.h"
+
+char sysexData[128];
+
+void getMIDIMessage()
+{
+    unsigned char st = midis.getc();
+    switch(st >> 4) {
+        case 0x8:
+            midiNoteOff(st & 0xf, midis.getc(), midis.getc());
+            break;
+        case 0x9:
+            midiNoteOn(st & 0xf, midis.getc(), midis.getc());
+            break;
+        case 0xa:
+            midiPolyphonicKeyPressure(st & 0xf, midis.getc(), midis.getc());
+            break;
+        case 0xb:
+            char b2 = midis.getc();
+            if (b2 >= 120) {
+                midiChannelMode(st & 0xf, b2, midis.getc());
+            } else {
+                midiControlChange(st & 0xf, b2, midis.getc());
+            }
+            break;
+        case 0xc:
+            midiProgramChange(st & 0xf, midis.getc());
+            break;
+        case 0xd:
+            midiChannelPressure(st & 0xf, midis.getc());
+            break;
+        case 0xe:
+            char LSB = midis.getc();
+            char MSB = midis.getc();
+            midiPitchBend(st & 0xf, ((LSB << 7) | (MSB << 7)) + 8192);
+            break;
+        case 0xf:
+            int t2 = st & 0xf;
+            if (t2 <= 7) {
+                getMIDISystemCommonMessage(t2);
+            } else {
+                midiSystemRealtimeMessage(st);
+            }
+            break;
+    }
+}
+
+void getMIDISystemCommonMessage(char t2)
+{
+    switch(t2) {
+        case 0:
+            //もう面倒臭いから128byteのバッファに適当に放り込んでおこう
+            sysexData[0] = 0xf0;
+            int i = 1;
+            char d = 0;
+            do {
+                d = midis.getc();
+                sysexData[i]=d;
+                i++;
+            } while(d != 0xf7);
+
+            break;
+        case 1:
+            midis.getc();
+            break;
+        case 2:
+            midis.getc();
+            midis.getc();
+            break;
+        case 3:
+            midis.getc();
+            break;
+        case 4:
+            break;
+        case 5:
+            break;
+        case 6:
+            break;
+        case 7:
+            //どうしろと
+            break;
+    }
+}
+
+void midiSystemRealtimeMessage(char mes)
+{
+
+}
+
+
+void midiNoteOn(char ch, char note, char vel)
+{
+    if (vel==0) {
+        globalrelease((ch<<8)|note);
+    } else {
+        globalattack((ch<<8)|note,vel);
+    }
+}
+
+void midiNoteOff(char ch, char note, char vel)
+{
+    globalrelease((ch<<8)|note);
+}
+
+void midiPolyphonicKeyPressure(char ch, char note, char vel)
+{
+
+}
+
+void midiControlChange(char ch, char ctrl, char data)
+{
+
+}
+
+void midiChannelMode(char ch, char ctrl, char data)
+{
+
+}
+
+void midiProgramChange(char ch, char prg)
+{
+
+}
+
+void midiChannelPressure(char ch, char pres)
+{
+
+}
+
+void midiPitchBend(char ch, short pb)
+{
+
+}
+
+void midiSystemExclusiveMessage()
+{
+
+}
\ No newline at end of file
diff -r 0ffdefe75566 -r 62da91a1eaf1 MIDI.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MIDI.h	Mon Dec 29 12:48:44 2014 +0000
@@ -0,0 +1,16 @@
+#pragma once
+#include "Main.h"
+
+extern Serial midis;
+void getMIDIMessage();
+void getMIDISystemCommonMessage(char t2);
+void midiSystemRealtimeMessage(char mes);
+void midiNoteOn(char ch, char note, char vel);
+void midiNoteOff(char ch, char note, char vel);
+void midiPolyphonicKeyPressure(char ch, char note, char vel);
+void midiControlChange(char ch, char ctrl, char data);
+void midiChannelMode(char ch, char ctrl, char data);
+void midiProgramChange(char ch, char prg);
+void midiChannelPressure(char ch, char pres);
+void midiPitchBend(char ch, short pb);
+void midiSystemExclusiveMessage();
diff -r 0ffdefe75566 -r 62da91a1eaf1 MIDIUtil.h
--- a/MIDIUtil.h	Mon Dec 29 09:01:10 2014 +0000
+++ b/MIDIUtil.h	Mon Dec 29 12:48:44 2014 +0000
@@ -4,4 +4,34 @@
 
 inline float getNoteNumberFrequency(int nn) {
     return notenumfreqs[nn];
-}
\ No newline at end of file
+}
+
+enum MIDIMessageType{
+    NoteOn,
+    NoteOff,
+    PolyphonicKeyPressure,
+    ControlChange,
+    ProgramChange,
+    ChannelPressure,
+    PitchBend,
+    ChannelMode,
+    SystemCommon,
+    SystemRealtime,
+};
+
+typedef struct {
+    MIDIMessageType type;
+    char channel;
+    union {
+        char note;
+        char control;
+        char program;
+        char LSB;
+    };
+    union {
+        char velocity;
+        char pressure;
+        char data;
+        char MSB;
+    };
+} MidiMessage;
\ No newline at end of file
diff -r 0ffdefe75566 -r 62da91a1eaf1 Main.cpp
--- a/Main.cpp	Mon Dec 29 09:01:10 2014 +0000
+++ b/Main.cpp	Mon Dec 29 12:48:44 2014 +0000
@@ -14,13 +14,15 @@
 const double smpus = 1000000.0 / smpps;
 
 double ntime = 0.0;
-double freq = 2.5f;
+volatile double freq = 0;
 
 float out = 0.0f;
 
 void tick_sampling();
 void midiReceived();
 
+int state[operators];
+
 int main()
 {
     //DAC setting
@@ -30,26 +32,50 @@
     output.referenceMode(MCP4922::DAC_B, MCP4922::REF_UNBUFFERED);
     output.gainMode(MCP4922::DAC_B, MCP4922::GAIN_1X);
     output.powerMode(MCP4922::DAC_B, MCP4922::POWER_NORMAL);
-    
+
     //MIDI Serial setting
     midis.baud(256000);
     midis.format();
-    
+
     //for(int i = 0; i < operators; i++) new(op + i) FMOperator(&master, &t);
-    sampler.attach_us(&tick_sampling, smpus);
     midis.attach(&midiReceived);
-    
+
     master.start();
-    while(true);
-}
-
-void tick_sampling()
-{
-    ntime = master.read_us()/1000000.0;
-    output.write(MCP4922::DAC_A, out);
+    while(true) {
+        ntime = master.read_us()/1000000.0;
+        float out = 0;
+        int oc=0;
+        for(int i=0;i<operators;i++) {
+            if (state[i]!=0) {
+                oc++;
+                out += t.sin(M_PI * 2.0 * ntime * getNoteNumberFrequency(state[i]&0x7f));
+            }
+        }
+        output.write(MCP4922::DAC_A,out/oc+0.5);
+    }
 }
 
 void midiReceived()
 {
-    out = 1.0f - out;
+    getMIDIMessage();
+    while(midis.readable()) midis.getc();
+}
+
+void setDebugFrequency(double f)
+{
+    freq = f;
+}
+
+void globalattack(unsigned short st,char vel) {
+    int to=0;
+    for(int i = 0; i < operators; i++) {
+        if (state[i]==0) to=i;
+    }
+    state[to] = st | vel << 16;
+}
+
+void globalrelease(unsigned short st) {
+    for(int i = 0; i < operators; i++) {
+        if ((state[i]&0xffff)==st) state[i]=0;
+    }
 }
\ No newline at end of file
diff -r 0ffdefe75566 -r 62da91a1eaf1 Main.h
--- a/Main.h	Mon Dec 29 09:01:10 2014 +0000
+++ b/Main.h	Mon Dec 29 12:48:44 2014 +0000
@@ -3,17 +3,19 @@
 #include <string>
 #include <new>
 #include "mbed.h"
-#include "USBDevice.h"
-#include "USBMIDI.h"
 #include "MCP4922.h"
 #include "AOTTrigon.h"
 #include "I2CEEPROM.h"
+#include "MIDI.h"
 #include "MIDIUtil.h"
 #include "Operator.h"
 
 #define M_PI 3.14159265358f
 
-
 #define NUFM401_VENDOR 0x0801
 #define NUFM401_PRODUCT 0x0201
-#define NUFM401_RELEASE 0x0001
\ No newline at end of file
+#define NUFM401_RELEASE 0x0001
+
+void setDebugFrequency(double f);
+void globalattack(unsigned short st,char vel);
+void globalrelease(unsigned short st);
\ No newline at end of file