Since all melodies are made up of a series of intervals, it is essential to learn to identify intervals by ear and be able to reproduce them at will both with your voice and on an instrument to check them against.
If you can sing something accurately, it means you are hearing it accurately. Here are some ways of developing your ability to identify and reproduce intervals. The example given in the first two exercises is a minor 3rd, but it is essential to go through these processes with all intervals.
1. Choose an interval you wish to work on (e.g., minor 3rds). Play a starting note (e.g., C) and sing it. Then sing a minor 3rd up from that note (Eb). Hold the note with your voice while you test its accuracy on your instrument.
Then choose another starting note and repeat the process. Keep doing this until you are accurate every time. The next step is to sing the interval (in this case a minor 3rd) downwards from your starting note. Again, do this repeatedly until you are accurate every time.
2. Sing the same interval consecutively upwards and then downwards several times. E.g., start on C and sing a minor 3rd up from it (Eb). Then sing a minor 3rd up from Eb (Gb). Then another minor third up from Gb (Bbb - which is enharmonically the same as A).
Then up another minor 3rd (C an octave higher than the starting note). Once you can do this, reverse the process (Start on C and sing a minor 3rd down to A, then another minor 3rd down and then another, etc).
3. Play and sing a starting note (e.g., C) and then think of it as the first degree of the chromatic scale - sing "one". Now sing the flattened second degree of the scale - sing "flat two". This note is a minor 2nd up from your C note (a Db note). Then sing the C again ("one").
Then sing the second degree of the scale (a D note - sing "two"). Next, sing your C note again ("one"). Continue in this manner all the way up the chromatic scale until you reach C an octave above. The entire sequence goes: 1, b2, 1, 2, 1, b3, 1, 3, 1, 4, 1, b5, 1, 5, 1, b6, 1, 6, 1, b7, 1, 7, 1, 8, 1.
As with the previous exercises, once you can do this accurately (check your pitches on your instrument), reverse the process and sing downwards from the top of the scale, working your way down the chromatic scale again. The downward sequence goes 1(8), 7, 1, b7, 1, 6, 1, b6, 1, 5, 1,b5, 1, 4, 1, 3, 1, b3, 1, 2, 1, b2, 1, 1, 1(8).
4. As well as hearing intervals melodically (one note at a time), it is important to be able to hear them harmonically (two notes played together). A good way to develop this is to have a friend play random harmonic intervals on either guitar or keyboard while you identify them. Keep your back to the instrument while you do this, so that you cannot identify the intervals by sight.
It is important to work at these things regularly until they become easy. Don't get frustrated if you can't hear intervals accurately at first. Most people have trouble with this. If you work at it for several months, you will see a dramatic improvement in your musical hearing, and will be able to improvise much more freely as well as being able to work out parts from recordings more easily.