[c]Ricezione di program change midi da keyboard a pc

di il
1 risposte

[c]Ricezione di program change midi da keyboard a pc

Ciao.
Mi addentro in un discorso un po' particolare in quanto io sono programmatore ma per hobby sono anche musicista (suono tastiere elettroniche).
Ora io ho una tastiera che può essere collegata al pc via usb e viene vista come una periferica midi.
La tastiera quando si fanno certe operazioni invia dei messaggi midi di program change agli altri dispositi collegati alle sue porte midi.
Ora immaginiamo che al posto di collegare un altro strumento midi alla tastiera , io collego le uscite midi della mia tastiera agli ingressi midi di una scheda audio usb esterna (m-audio audiophile usb).
Ecco la domanda. Come faccio tramite linguaggio C/ C++ (uso c++ builder 6) a scrivere un programma che acceda alla periferica midi vista da windows e si metta in ascolto dei messaggi midi che arrivano (program change), e mi scriva tutti questi messaggi (normalmente codici alfanumerici) a video? è possibile?
Praticamente La tastiera quando premo il tasto incrimato manda un messaggio di program change (penso sulla porta midi keyboard). leggendo il manuale questo messaggio dovrebbe contenere anche un numero che è quello che mi interessa recuperare da programma...
Come procedo?
Considerato che collego la tastiera nei midi della scheda audio esterna audiophile usb come faccio a eccedervi da programma?
come richiamo da pc la periferica audiophile usb sezione midi da c++?
qualcuno ha già fatto di queste prove?
ho bisogno di qualcuno che mi aiuti non so da che parte cominciare.
PS spulciando il web sono riuscito a scrivere questo codice:

#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
#include <conio.h> /* include for kbhit() and getch() functions */
#include <stdio.h> /* for printf() function */
#include <windows.h> /* required before including mmsystem.h */
#include <mmsystem.h> /* multimedia functions (such as MIDI) for Windows */
int main(int argc, char* argv[])
{

int ckey; // storage for the current keyboard key being pressed
int notestate = 0; // keeping track of when the note is on or off
int velocity = 100; // MIDI note velocity parameter value
int midiport; // select which MIDI output port to open
int flag; // monitor the status of returning functions
HMIDIOUT device; // MIDI device interface for sending MIDI output
MIDIINCAPS mic;
unsigned long iNumDevs, i;
// variable which is both an integer and an array of characters:
union { unsigned long word; unsigned char data[4]; } message;
// message.data[0] = command byte of the MIDI message, for example: 0x90
// message.data[1] = first data byte of the MIDI message, for example: 60
// message.data[2] = second data byte of the MIDI message, for example 100
// message.data[3] = not used for any MIDI messages, so set to 0
message.data[0] = 0x90; // MIDI note-on message (requires to data bytes)
message.data[1] = 60; // MIDI note-on message: Key number (60 = middle C)
message.data[2] = 100; // MIDI note-on message: Key velocity (100 = loud)
message.data[3] = 0; // Unused parameter
/* Get the number of MIDI In devices in this computer */
iNumDevs = midiInGetNumDevs();

/* Go through all of those devices, displaying their names */
for (i = 0; i < iNumDevs; i++)
{
/* Get info about the next device */
if (!midiInGetDevCaps(i, &mic, sizeof(MIDIINCAPS)))
{
/* Display its Device ID and name */
printf("Device ID #%u: %s\r\n", i, mic.szPname);
}
}


// Assign the MIDI output port number (from input or default to 0)
if (argc < 2) {
midiport = 0;
} else {
midiport = atoi(argv[1]);
}
printf("MIDI output port set to %d.\n", midiport);

// Open the MIDI output port
flag = midiOutOpen(&device, midiport, 0, 0, CALLBACK_NULL);
if (flag != MMSYSERR_NOERROR) {
printf("Error opening MIDI Output.\n");

}

// Main event loop

while (1) { // event loop
ckey = getch();

flag = midiOutShortMsg(device, message.word);
if (flag != MMSYSERR_NOERROR) {
printf("Warning: MIDI Output is not open.\n");
}

}


// turn any MIDI notes currently playing:
midiOutReset(device);

// Remove any data in MIDI device and close the MIDI Output port
midiOutClose(device);

return 0;
}

devo ancora testarlo con la periferica però mi domando una cosa
1. Come faccio nel codice a sciegliere la scheda audio esterna?
2. Come faccio a leggere se è stato inviato un program change e cosa contiene quel messaggio? Grazie!
penso dovrei leggere il message data che mi invia la tastiera ma non come fare....
A me interessa leggere i messaggi che arrivano di program change
grazie!
PS Io conosco bene solo c e c++ ma sono disposto a cambiare linguaggio se facilità il raggiungimento del mio scopo e se mi spiegate come fare....
Grazie!

1 Risposte

Devi accedere o registrarti per scrivere nel forum
1 risposte