A simple music converter/generator

2021-08-21

This article talks about one of my little projects: a program that can convert a text music sheet, in a custom, arbitrary format, to audio files.

Motivation

I watched a video on YouTube which explains how to generate music from scratch, using Haskell. Then I thought that I could write this simple program in C to practice my C programming skills.

And later I added the feature to read a plain text music sheet, as I thought it to be more flexible this way.

Repository

The source codes of this simple project are stored in the git repository. Since this is a hobby project, there are no documentations whatsoever, so it might not be a good experience to read the codes. Sorry for that. ;P.

Examples

Here are two example files for demonstration purposes.

Note: The audio of the marble machine song is based on the piano version, not on the original version.

How does it work?

Basic principle

A sound is represented as a wave. It consists of a series of numbers. And that is what the program generates: a sequence of numbers.

All we have to do is to calculate the frequency and the magnitude of the wave, and then produce the correct numbers at the correct moments.

Timbre

The hard part is how to simulate a specific timbre, like a piano's. This is in general quite difficult, since the timbres of music instruments in general are not constant: they can vary from time to time and from frequency to frequency, which comprise the overall impression of the timbre. This is especially for violin-like instruments, since the vibration pattern of it is always changing. This means that even if we manage to approximate the timbre of a violin at one specific frequency, it won't sound right at another frequency.

Fortunately, the piano's timbre is quite stable, so it still sounds pleasing to simulate its timbre. Through my experiments, I found that the key to simulating the piano's timbre is that the magnitude of the sound should decay exponentially. Without this decay pattern, it won't sound right.

Future

Again, this is a hobby project, and there is nothing exceptional in it. But I have some ideas to try out in the future.

  • Use a partial differential equation to simulate the violin's timbre. This is the best way I can think of in order to simulate violin's timbre.
  • Read music sheets in other more widely used formats, such as Lilypond's format, MIDI files, or directly from photos of music sheets. This can greatly ease the process of producing the audio file from musics. Sadly these are beyond my interests and capabilities now.
  • Draw an imaginary piano which is automatically "playing", in sync with the audio. With this one can create demo videos of musics easily. This is not hard to do, but I currently see no interest in doing so. ;P

All original content is licensed under the free copyleft license CC BY-SA .

Author: JSDurand

Email: durand@jsdurand.xyz

Date: 2021-08-21 Sam 11:57:00 CST

GNU Emacs 29.3.50 of 2024-04-07 (Org mode 9.6.15)

Validate