Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to generate note with some frequency ????
thank for help
Posted
Comments
Richard MacCutchan 28-Sep-11 12:00pm    
Use Notepad once a day.
Bala Selvanayagam 28-Sep-11 12:04pm    
Can you just go into bit detail on this please ?
shadiabusamra 28-Sep-11 12:09pm    
for example i need to generate 550hz by MIDI ??
Sergey Alexandrovich Kryukov 28-Sep-11 12:55pm    
You can only generate the closest note: 554.3. By the way, this is the note C# (do-dies)! What a coincidence!
This is not what MIDI does. Please read my solution.
--SA

With MIDI, you do not generate frequency, you generate a chromatic notes on some instrument, using 12-pitch-per-octave, equal temperament tuning system.

On top of this, you can use some effects like wheel effect ("bending" like on guitar is imitated by a special wheel on some controllers). With such effect, the frequency deviates from its chromatic value, but you cannot generate arbitrary frequency, so the effects you can generate are really limited.

Main MIDI commands are like "play note start" and "stop playing a note", like a key down/up in a organ-type controller, and the number passed to the MIDI interface is just a "number of a note" in a half-tone step, like in modern European equal temperament based notation. You also control volume and tempo throughout a play. A MIDI-encoded play closely matches the piece of scores written in traditional modern musical notation as it is described here: http://en.wikipedia.org/wiki/Musical_notation[^].

There are number of software products rendering MIDI files using traditional modern musical notation; they may support editing of music play in this or some alternative notations, play it, save/open resulting document and, ultimately, export the document to the MIDI file format. You can also record the play using all kinds of (hardware) MIDI-controllers.

See http://en.wikipedia.org/wiki/Midi[^].

What do you use for MIDI implementation?
You can read these articles and try:
Wrapper Library for Windows MIDI API[^],
C# MIDI Toolkit[^].

In my opinion, as you cannot produce really unique sound, all this is not really good to create real "analog" music good for performance, say, for writing and distributing audio CDs. If you listen MIDI music for a long time you may feel irritated by its "mechanical" sound, because all the sounds are essentially repeated many time without having unique sounds typical for life music.

Rather, this is the excellent machinery for working with "musical calculus" (and only in one equal temperament), for composers, exchange of musical compositions without participation of life musicians, presenting them in the form any musician can understand and reproduce and also for learning music and training.

Using the libraries found through the libraries I referenced above, you can create software with all of the features I mentioned above; you just need to understand very elementary basics of music theory and have programming experience.

Good luck,
—SA
 
Share this answer
 
v3
Comments
shadiabusamra 28-Sep-11 13:03pm    
thank ...
Don't want random frequency but i need note between c and d in piano((frequency c + frequency d)/2)
Sergey Alexandrovich Kryukov 28-Sep-11 14:37pm    
Wrong!!!

Until you learn a bit of acoustics, elementary math and very basic music you won't be able to do anything.
Half-tone is not addition of frequency! its multiplication by square root of 12-power of 2, 2^(1/12)!
Between C and D is C#, 554.3 Hz, NOT 550 Hz!

Do I have to explain such a simple thing as temperament?! Or you can write elementary equation by yourself?

Look, one octave is frequency * 2, right? Any two nearest half-tones t1/t2 always gives the same ratio, t1/t2=const R, right? Now, make an elementary equation and find R. Then make a formula how to find frequency between some f1 and f2 if there is N half-tones between them. Done?

And please finally accept my solution formally (green button) -- you COULD use MIDI after you understand all that.
MIDI gives you all 12 half-tones, not just A B C D E F G notes.

--SA
phil.o 29-Sep-11 12:59pm    
That should be noted 6 ;)
Simon Bang Terkildsen 28-Sep-11 15:36pm    
My 5
Sergey Alexandrovich Kryukov 28-Sep-11 15:41pm    
Thank you, Simon, time to play some tune. :-)
--SA
To play C# (next to Middle C) using Midi:
C#
using System;
using System.Runtime.InteropServices;

namespace Midi
{
    // http://msdn.microsoft.com/en-us/library/dd798478(v=VS.85).aspx
    /*
    void CALLBACK MidiOutProc(
      HMIDIOUT hmo,
      UINT wMsg,
      DWORD_PTR dwInstance,
      DWORD_PTR dwParam1,
      DWORD_PTR dwParam2
    );
     */
    internal delegate void MidiOutProc(IntPtr hmo, int wMsg, IntPtr dwInstance, IntPtr dwParam1, IntPtr dwParam2);

    internal static class NativeMethods
    {
        public const int CALLBACK_FUNCTION = 0x00030000;
        public const int MMSYSERR_NOERROR = 0;

        // http://msdn.microsoft.com/en-us/library/dd798468(v=VS.85).aspx
        /*
    MMRESULT midiOutClose(
      HMIDIOUT hmo
    );
         */
        [DllImport("Winmm.dll")]
        public static extern int midiOutClose(IntPtr hmo);

        // http://msdn.microsoft.com/en-us/library/dd798472(v=VS.85).aspx
        /*
    UINT midiOutGetNumDevs(void);
         */
        [DllImport("Winmm.dll")]
        public static extern int midiOutGetNumDevs();

        // http://msdn.microsoft.com/en-us/library/dd798476(v=VS.85).aspx
        /*
        MMRESULT midiOutOpen(
          LPHMIDIOUT lphmo,
          UINT uDeviceID,
          DWORD_PTR dwCallback,
          DWORD_PTR dwCallbackInstance,
          DWORD dwFlags
        );
         */
        [DllImport("Winmm.dll")]
        public static extern int midiOutOpen(out IntPtr lphmo, int uDeviceID, MidiOutProc dwCallback, IntPtr dwCallbackInstance, int dwFlags);

        // http://msdn.microsoft.com/en-us/library/dd798481(v=VS.85).aspx
        /*
    MMRESULT midiOutShortMsg(
      HMIDIOUT hmo,
      DWORD dwMsg
    );
         */
        [DllImport("Winmm.dll")]
        public static extern int midiOutShortMsg(IntPtr hmo, int dwMsg);
    }
}

C#
using System;
using System.Threading;

namespace Midi
{
    public class MidiTest
    {
        private MidiOutProc midiOutProc;

        public MidiTest()
        {
            midiOutProc = new MidiOutProc(Callback);
            if (NativeMethods.midiOutGetNumDevs() > 0)
            {
                IntPtr handle;
                if (NativeMethods.midiOutOpen(out handle, 0, midiOutProc, IntPtr.Zero, NativeMethods.CALLBACK_FUNCTION) == NativeMethods.MMSYSERR_NOERROR)
                {
                    int noteOnChannel1 = 0x90; // channel - 1
                    int noteNumber = 0x3D; // C# above middle C
                    int velocity = 0x64; // 100 (127 [7F] is Max)
                    int data = noteOnChannel1 | (noteNumber << 8) | (velocity << 16);
                    NativeMethods.midiOutShortMsg(handle, data);

                    Thread.Sleep(1000); // play note for one second

                    int noteOffChannel1 = 0x80; // channel - 1

                    data = noteOffChannel1 | (noteNumber << 8) | (velocity << 16);
                    NativeMethods.midiOutShortMsg(handle, data);

                    NativeMethods.midiOutClose(handle);
                }
            }
        }

        private void Callback(IntPtr hmo, int wMsg, IntPtr dwInstance, IntPtr dwParam1, IntPtr dwParam2)
        {
            //
        }
    }
}

Now just create a new MidiTest instance and you will get a C#!
C#
new MidiTest();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900