0

I want to play each note of a .mid file separately. I am using android-mid-lib for android(Click here).

But I am not able to play the notes seprately as i am not able to get each note of .mid files separately. Below is what I have done so far.

File input = new File(Environment.getExternalStorageDirectory()+"/ants.mid");
      //  File input= new File("android.resource://" + getPackageName() + "/" + R.raw._twinkle_twinkle_bundled);
        try {
            midiFile = new MidiFile(input);
            System.out.println("asdsadsadsa:"+midiFile.getTracks().get(0).getEvents().first().getDelta());


        } catch (IOException e) {
            e.printStackTrace();
        }
        // 1. Create some MidiTracks
        MidiTrack tempoTrack = new MidiTrack();
        MidiTrack noteTrack = new MidiTrack();


// 2. Add events to the tracks
// Track 0 is the tempo map
        TimeSignature ts = new TimeSignature();
        ts.setTimeSignature(4, 4, TimeSignature.DEFAULT_METER, TimeSignature.DEFAULT_DIVISION);

        Tempo tempo = new Tempo();
        tempo.setBpm(228);

        tempoTrack.insertEvent(ts);
        tempoTrack.insertEvent(tempo);

// Track 1 will have some notes in it
        final int NOTE_COUNT = 80;

        for(int i = 0; i < NOTE_COUNT; i++)
        {
            int channel = 0;
            int pitch = 1 + i;
            int velocity = 100;
            long tick = i * 480;
            long duration = 120;

            noteTrack.insertNote(channel, pitch, velocity, tick, duration);
        }

// 3. Create a MidiFile with the tracks we created
        ArrayList<MidiTrack> tracks = new ArrayList<MidiTrack>();
        tracks.add(tempoTrack);
        tracks.add(noteTrack);

        MidiFile midi = new MidiFile(MidiFile.DEFAULT_RESOLUTION, tracks);

// 4. Write the MIDI data to a file
        File output = new File(Environment.getExternalStorageDirectory()+"/exampleout.mid");
        try
        {
            midi.writeToFile(output);
        }
        catch(IOException e)
        {
            System.err.println(e);
        }

Using this I am able to change the pitch of whole mid file but I am not getting how to play each note separately.
Any help is appreciated!

Asad AndyDev
  • 481
  • 5
  • 23
  • What do you mean with "separately"? – CL. Oct 17 '16 at 06:50
  • I meant like there is a .mid file named _twinkle_twinkle_little_stars.mid and the poem is played using the piano. Now what i want is to play those piano notes separately mean first sound, than second and so on.. – Asad AndyDev Oct 17 '16 at 07:13
  • Playing sounds after each other is what a normal .mid player already does. What is the difference you want? – CL. Oct 17 '16 at 07:15
  • @CL. I want to play them myself mean on clicking first button I want to play first sound then on clicking second button it plays second sound. – Asad AndyDev Oct 17 '16 at 07:27
  • This cannot be done with a single .mid file. – CL. Oct 17 '16 at 07:28
  • okay @CL. mean we can not just read a single .mid file and play its adjacent notes one by one ? – Asad AndyDev Oct 17 '16 at 07:33
  • 1
    See the questions listed under "Related". – CL. Oct 17 '16 at 07:38
  • 1
    Seems possible. The "android-mid-lib" library allows manipulating MIDI files which includes extracting single events of a single track. The most interesting events are the `Note ON` events which start the playback of a single note. (Simultaneous notes on the same track could be somewhat of a problem of course.) The options for playing back single notes are a) create a new MIDI file for each single note and play them back b) [play single notes with the "Android midi driver" library](http://stackoverflow.com/questions/36193250/android-6-0-marshmallow-how-to-play-midi-notes/36517164#36517164). – Markus Kauppinen Oct 17 '16 at 08:44
  • @MarkusKauppinen yeah it seems the right way too. Thanks for showing me one more possibility. – Asad AndyDev Oct 17 '16 at 09:26

0 Answers0