# Piano Synthesizer Tutorial

Ning Hu and Roger B. Dannenberg

This page describes how to use the piano synthesizer in Nyquist. Both SAL and Lisp syntax are used. Lisp syntax is in small type.

First, load the file "pianosyn.lsp" to initialize the synthesizer. The file is in the "lib" directory.

`> load "pianosyn"
` > `(``load``"pianosyn"``)`

After the piano synthesizer has been initialized and returns, you can use the functions provided by the synthesizer program to generate piano sounds.

The functions are shown as below:

## Generate a single note

Use the function:

`piano-note(duration, step, dynamic)`

`(piano-note duration step dynamic)`

- duration represents the duration of the note in seconds.
- step represents the pitch of note, and uses step values as in all Nyquist functions. For example, value 60 is middle C (C4).
- dynamic means the dynamics of the note, and it ranges from 0 to 127. In a midi file, it is used as the velocity of the note.

This behavior returns a sound. You can play the sound.

Examples:

    > play piano-note(4, 60, 100)

    > (play (piano-note 4 60 100))

The commands above will generate and immediately play a C4 note that lasts 4 seconds, velocity at 100.

## Synthesize piano music according to a midi file

To convert a midi file to sound, use the function:

    piano-midi(midi-file-name)

    (piano_midi midi_file_name)

- `midi-file-name` is a string representing the name of the midi file.

This behavior will generate sound according to the score of the midi file. It generates stereo sound, and the sample rate is the default sound sample rate.

## Convert midi file to sound file

To convert a midi file to a sound file using the piano synthesizer, use the function:

    piano-midi2file(midi-file-name, sound-file-name)

    (piano-midi2file midi-file-name sound-file-name)

- `midi-file-name` is a string representing the name of the midi file.
- `sound-file-name` is a string representing the name of the sound file.

This command will generate a sound file according to the score of the midi file and play it at the same time. It generates stereo sound, and the sample rate is the default sound sample rate.

### Example:

    > exec piano-midi2file("demo.mid", "demo.wav")

    > (piano-midi2file "demo.mid" "demo.wav")

- "demo.mid" is the name of the midi file. It saves the output sound into "demo.wav", and plays it when generating the output.