fbpx
Skip to main content

MIDI Forum

Need code to "play ...
 
Notifications
Clear all

Need code to "play piano note(s)" on different instrument sounds set

9 Posts
4 Users
0 Reactions
6,357 Views
Vu
 Vu
Posts: 10
Active Member
Topic starter
 

Hello,

I have a M-Audio controller keyboard, I am looking for a sample code that to play any note/key input on selected instruments sounds set (e.g: 3-Electric Grand Piano, 25-Acoustic Nylon Guitar, ... where 3, 25 are General MIDI instrument codes number).
I did a lot of research and tried several examples but no luck.....

Below are one of example I tried, but no luck, your guide, help would be greatly appreciated.
Thank you.
Vu.

//var context = new AudioContext(),
// oscillators = {};

//if (navigator.requestMIDIAccess) {
// navigator.requestMIDIAccess()
// .then(success, failure);
//}

//function success(midi) {
// var inputs = midi.inputs.values();
// // inputs is an Iterator

// for (var input = inputs.next() ; input && !input.done; input = inputs.next()) {
// // each time there is a midi message call the onMIDIMessage function
// input.value.onmidimessage = onMIDIMessage;
// }
//}

//function failure() {
// console.error('No access to your midi devices.')
//}

//function onMIDIMessage(message) {
// var frequency = midiNoteToFrequency(message.data[1]);
// if (message.data[0] === 144 && message.data[2] > 0) {
// playNote(frequency);
// }

// if (message.data[0] === 128 || message.data[2] === 0) {
// stopNote(frequency);
// }
//}

//function midiNoteToFrequency(note) {
// return Math.pow(2, ((note - 69) / 12)) * 440;
//}

//function playNote(frequency) {
// oscillators[frequency] = context.createOscillator();
// oscillators[frequency].frequency.value = frequency;
// oscillators[frequency].connect(context.destination);
// oscillators[frequency].start(context.currentTime);
//}

//function stopNote(frequency) {
// oscillators[frequency].stop(context.currentTime);
// oscillators[frequency].disconnect();
//}

 
Posted : 30/01/2021 1:58 pm
Eddie Lotter
Posts: 295
Reputable Member
 

It is not clear what you are ultimately trying to achieve.
Are you wanting to play a single note and have that note play on multiple instruments simultaneously?

 
Posted : 30/01/2021 2:57 pm
Vu
 Vu
Posts: 10
Active Member
Topic starter
 

Thank you for responding,
I need to play any note(s) that having selected sound out put as I want:

For instance:
I have a drop-down list of Sound Sets: (3-Electric Grand Piano, 25-Acoustic Nylon Guitar etc....)
If I selected "Electric Grand Piano" from that list.
Then I want to play note (say "C4" ) that give me out put of sound C4-"Electric Grand Piano"....

Thank you.

 
Posted : 30/01/2021 3:12 pm
Eddie Lotter
Posts: 295
Reputable Member
 

Okay, so you want to be able to select an instrument and then play that instrument.

Next question: Do you need to do it programmatically or is it enough to configure your M-Audio controller to allow you to select the instrument using a slider or rotary encoder?

 
Posted : 30/01/2021 5:46 pm
Vu
 Vu
Posts: 10
Active Member
Topic starter
 

Thank you for responding,

I need to programmatically!.

I have a question as for 2nd option you mentioned thought: does it mean, if I have different controller (let say AKAI-Controller), it will give me different instrument using slider or rotary encoder?
If you could also provide me the information how to do this second option that would be supper good!.

Mean time, I am trying to understand the example from the link below, that might give me a good idea...
https://andyhall.github.io/webaudio-instruments/

Thank you very much in advance!.
Vu.

 
Posted : 30/01/2021 6:17 pm
Sema
 Sema
Posts: 179
Reputable Member
 

If you need a JavaScript synth with GM sounds, please have a look at https://github.com/jazz-soft/JZZ-synth-Tiny

 
Posted : 30/01/2021 8:09 pm
Eddie Lotter
Posts: 295
Reputable Member
 

if I have different controller (let say AKAI-Controller), it will give me different instrument using slider or rotary encoder?

The idea is to send a standard Program Change message using the slider or rotary encoder. This selects the instrument in the target MIDI device. See the MIDI specification for details.

Unfortunately, prior to the General MIDI standard, different MIDI devices have different instrument lists, so the same program number sent to two different devices might select two completely different instruments.

You will find a lot of MIDI code on github using many different programming languages. Choose the language and platform that you are most familiar with and start there.

 
Posted : 30/01/2021 9:25 pm
Geoff
Posts: 1040
Noble Member
 

Hello,

The code example you give, I suspect, is NOT trying to do anything like what you want.

I suspect that it's using a variety of C++. The program seems to be receiving a midi note number, and then converting the note number into a frequency, and then using an oscillator (not a midi instrument or suchlike) to create a sound. I don't see anything that might make the 'sound' be anything different for different instruments.

Your program needs more to receive a basic midi instruction, and then send that same instruction out again to your midi instrument. No oscillators involved. You then need additional code regarding instrument selection, and use of any relevant controllers.

Depending on the midi device involved, you might be able to use simple Program Change to select different instruments, or you might need to use Bank Select instructions as well.

I'm not sure if you need the midi input, and the keyboard controller, to achieve your purpose. I have written versions of software similar to this, for testing/auditioning sounds, and I have used the computer keyboard for input and converted the key pressed into midi note number via a table. Shift and key has generated a chord based on the selected note. Other key combinations has applied certain controllers, or changed volume/pan etc. You can make it all as fancy as you need.

On my various computers I may have different versions, at least one is specific to the Roland LAPC-I soundcard that I have, and the 1,000+ instrument patches that can be loaded. This is using just the midi ports to send out the midi data from the PC to the Roland card. Relatively simple changes could make the prog work with other devices.

Am I descibing something like what you were trying to achieve?

Geoff

 
Posted : 31/01/2021 7:53 am
Vu
 Vu
Posts: 10
Active Member
Topic starter
 

Hello,
Thank you all very much for responded.
I was able to find a great source (link below) and make it works the way I needed.

https://surikov.github.io/webaudiofont/

Thank you.
Vu.

 
Posted : 31/01/2021 5:03 pm
Share: