3

Greetings,

I'm working on a low level programming project and I want to play a music with the computer speaker.

I'm already capable of using the speaker (with timer2) and a song is represented in the following way:

note_t *music;

where note_t represents a note and it's compound by:

typedef struct {
  int freq; /* note frequency */
  int dur;  /* note duration in miliseconds */
} note_t;

Now, what would be the best way to get the frequencies and durations of the notes from a music file?

Thanks in advance!

EDIT

To clarify some doubts, what I want to know is the best format to get the necessary information to create a song with the structure above indicated.

Renato Rodrigues
  • 1,038
  • 1
  • 18
  • 35

2 Answers2

2

Depending on your exact purpose, you can use one of the ringtone formats or invent your own.

An example simple ringtone format is RTTTL.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
1

anatolyg's answer is good. I just want to show how complex a task like this can be: have a look at MIDI for inspiration.

http://en.wikipedia.org/wiki/Musical_Instrument_Digital_Interface

MIDI files can be used as "virtual sheet music" for music software, storing the notes and a lot of additional information describing the nuances of playing (for example the velocity, pitch bend, modulation and so on). It was built for storing entire pieces of music with multiple instruments and polyphony.

Community
  • 1
  • 1
Scorchio
  • 2,763
  • 2
  • 20
  • 28
  • I was using a MIDI to txt converter (http://en.nemidi.com/conversor/mid2txt.html) - it creates a txt with a bunch of info including the MIDI notes and its durations. Then I converted the notes to the corresponding frequencies. The problem is that the resulting song was a lot faster thant the original MIDI. – Renato Rodrigues Dec 30 '10 at 22:51
  • Increase the duration of each frequencies. Say, multiply each duration by 0.7 to make each duration slower. This is however a trial and error. But, that is the least you can do. Or you can slow down your run-time by sleep() or similar function. – Neigyl R. Noval Jan 01 '11 at 13:39
  • 1
    @Neigyl: I would start with a prepared MIDI with known note lengths to calibrate my software - no trial and error there. – Scorchio Jan 01 '11 at 21:20
  • Yes. That's it. Trial and error is just a rough impractical practice. Thanks. – Neigyl R. Noval Jan 02 '11 at 05:19