LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Playing piano from the terminal (https://www.linuxquestions.org/questions/programming-9/playing-piano-from-the-terminal-838004/)

kjaer 10-14-2010 06:31 AM

Playing piano from the terminal
 
Hi guys.

In short, what I want to achieve is this:
echo "a b c a" | piano.sh
And hear it, like it was played on a piano.
As you can see, I really would like to keep it simple, it is for a school project, and this is a minor but important thing. I am getting the data from the serial port and that part is working.
I have been looking at software synthesizers etc. but again, I want it to be simple. :-)

Has anyone done anything similar or related to this? Is it at all possible?

Best regards
Kresten

mpagnan 10-14-2010 03:41 PM

The sound card drivers direct a sound card on what sounds/notes to play. So, you need to program something that will send the right instruction for each note to the sound card driver. You will need more than a script. The only sound that script languages like bash have been programmed to play is the monotone "beep". I am not sure whether an Amarok script will do what you need. A "how to" is at http://amarok.kde.org/wiki/Script-Wr...o#Introduction

neonsignal 10-14-2010 11:34 PM

Quote:

Originally Posted by kjaer (Post 4127024)
In short, what I want to achieve is this:
echo "a b c a" | piano.sh

It depends how much programming you want to do (and which language).

One way would be to generate midi notes; for example, in Python you could use a module such as pyfluidsynth, or even generate midi events directly from the incoming note list.

A quick and ugly way would be to create a whole lot of mp3 files with the different notes, and then use a shell script to run mpg321 on the ones that match the notes coming in!

x111 10-14-2010 11:48 PM

Quote:

Originally Posted by neonsignal (Post 4127893)
It depends how much programming you want to do (and which language).

One way would be to generate midi notes; for example, in Python you could use a module such as pyfluidsynth, or even generate midi events directly from the incoming note list.

A quick and ugly way would be to create a whole lot of mp3 files with the different notes, and then use a shell script to run mpg321 on the ones that match the notes coming in!

An interesting idea, for a console.
The use of MIDI is a traditional way, but I think if you can manage to somehow "mix" the sound of the mp3 notes (using PulseAudio or something like that), it will be much more real. The sound of MIDI notes is rather flat, you know.

I'm no expert on programming and sound stuff, but an enthusiast of music and the console :D. Please keep posting the result of your work here.

neonsignal 10-15-2010 01:20 AM

Just for fun, you can also use SoX to play notes in a script, taking notes on the standard input (separated by spaces or lf):
Code:

#!/bin/bash
while read -e input
do
  for i in $input
  do
      play -q -n synth 3 pluck %$(expr index aabccddeffgg $i - 1) &
      sleep 0.5
  done
done


mpagnan 10-15-2010 07:21 AM

neonsignal,
Thanks for that. I do not know how after many years I missed SoX, that I now find is a standard offering of many Linux / Unix distributions.


All times are GMT -5. The time now is 07:37 PM.