ALSA Music/MIDI Driver

Forum for technical discussions regarding development. If you have a general suggestion, problem or comment, please use one of the other forums.

Moderator: OpenTTD Developers

Post Reply
User avatar
bernhard84
Engineer
Engineer
Posts: 5
Joined: 18 Jul 2009 17:47
Location: Austria

ALSA Music/MIDI Driver

Post by bernhard84 »

Hi!

I just wrote a MIDI music driver for ALSA (Linux). A patch for openttd 0.7.1 is attached below...

As a SoundBlaster Live user I was never really happy about the way MIDI music was handled in the Linux version of OpenTTD. Actually my sound hardware is capable of producing high-quality MIDI sound, but it's hard/impossible to use it in OpenTTD. The only options for me so far were:
  • using timidity, a software synth which consumes lots of CPU but still sounds inferior than my wavetable sound card
  • using extmidi, an ugly hack which didn't work for me for some reason
Well, since Transport Tycoon is only half the fun without the music, I decided to write a "native music driver" for ALSA. It's the most complex music driver so far, because it has to handle "everything" (reading and parsing MIDI files, sending MIDI events, managing playback thread, selecting a suitable output port, etc) on its own. ALSA is really a programmer's nightmare...
But finally I can enjoy the music without the horrors of timidity or extmidi now. Hooray! :D

Needless to say, I'd love to see this driver being included in one of the next releases... 8)


Cheers,
Bernhard
Attachments
openttd-0.7.1-alsaseq-v2.diff
Patch for adding ALSA music driver. Use patch -p1 to apply. (Updated, v2)
(36.46 KiB) Downloaded 71 times
Last edited by bernhard84 on 21 Jul 2009 20:24, edited 1 time in total.
User avatar
Ammler
President
President
Posts: 953
Joined: 18 Jun 2006 18:18
Location: Switzerland
Contact:

Re: ALSA Music/MIDI Driver

Post by Ammler »

bernhard84 wrote: Needless to say, I'd love to see this driver being included in one of the next releases... 8)
Then you should patch trunk. ;-)

Greets
Ammler
User avatar
bernhard84
Engineer
Engineer
Posts: 5
Joined: 18 Jul 2009 17:47
Location: Austria

Re: ALSA Music/MIDI Driver

Post by bernhard84 »

Ammler wrote: Then you should patch trunk. ;-)
Hmm... interesting idea. If someone gives me a subversion account, I'll do it! :wink:
I just hope that nobody's gonna kill me if my code deletes your hard disk, tortures your kitten, sets your house on fire, ...
Terkhen
OpenTTD Developer
OpenTTD Developer
Posts: 1034
Joined: 11 Sep 2008 07:32
Location: Spain

Re: ALSA Music/MIDI Driver

Post by Terkhen »

Ammler means that, if you are looking forward to inclusion, you should create your patches against latest trunk version, not against stable versions. Only devs can commit code to trunk.
User avatar
bernhard84
Engineer
Engineer
Posts: 5
Joined: 18 Jul 2009 17:47
Location: Austria

Re: ALSA Music/MIDI Driver

Post by bernhard84 »

And I thought:
Wow, that's an OPEN source project! They just want ANYONE to commit on trunk! :oops:

OK, here's my patch against trunk.
Attachments
openttd-trunk-alsaseq-v2.diff
(37.08 KiB) Downloaded 63 times
Last edited by bernhard84 on 19 Jul 2009 23:28, edited 1 time in total.
Roujin
Tycoon
Tycoon
Posts: 1884
Joined: 08 Apr 2007 04:07

Re: ALSA Music/MIDI Driver

Post by Roujin »

bernhard84 wrote:And I thought:
Wow, that's an OPEN source project! They just want ANYONE to commit on trunk! :oops:

OK, here's my patch against trunk.
Haha, that would be too chaotic.. :lol: Actually the devs here are checking patches quite thoroughly before considering to put them in trunk. :)
You may also want to create a flyspray task (category Patch). There it's more likely to get a dev's attention. :)
* @Belugas wonders what is worst... a mom or a wife...
<Lakie> Well, they do the same thing but the code is different.

______________
My patches
check my wiki page (sticky button) for a complete list

ImageImage
ImageImageImageImageImageImageImage
TrueBrain
OpenTTD Developer
OpenTTD Developer
Posts: 1370
Joined: 31 May 2004 09:21

Re: ALSA Music/MIDI Driver

Post by TrueBrain »

Impressive.

One comment I have: please try to follow the coding style of OpenTTD. It is pretty important if you are serious about your patch :) Examples:

not 'void* ', but 'void *'
'//' to make a comment on the same line code is on
unneeded white lines

stuff like that ;)
The only thing necessary for the triumph of evil is for good men to do nothing.
peter1138
OpenTTD Developer
OpenTTD Developer
Posts: 1740
Joined: 30 Mar 2005 09:43

Re: ALSA Music/MIDI Driver

Post by peter1138 »

I can't really see the point of adding all the complexity of a MIDI file parser just for one output driver, when using extmidi, along with pmidi, works perfectly well.

Some other comments, ignoring code style:
* There's a complete byte-oriented file reader in there. We have one of those already.
* It's pretty impossible to auto-detect which ALSA port to use, and get it correct. pmidi's method works well; that is use either the explicitly supplied port (the driver system does support parameters) or use ALSA_OUTPUT_PORTS. It never auto-detects.
He's like, some kind of OpenTTD developer.
User avatar
bernhard84
Engineer
Engineer
Posts: 5
Joined: 18 Jul 2009 17:47
Location: Austria

Re: ALSA Music/MIDI Driver

Post by bernhard84 »

OK, I updated the patch (see my previous posting).

Changes: fixed reading ALSA_OUTPUT_PORTS environment variable, refined auto-detection (also detects fluidsynth and timidity now), try to comply with OpenTTD coding standard (probably still not perfect yet).
petern wrote: There's a complete byte-oriented file reader in there. We have one of those already.
OK, I'll have a look at it later.
petern wrote: It's pretty impossible to auto-detect which ALSA port to use, and get it correct. pmidi's method works well; that is use either the explicitly supplied port (the driver system does support parameters) or use ALSA_OUTPUT_PORTS. It never auto-detects.
Well, probably it's impossible to get auto-detection right in 100% of all possible cases. But that's not the aim of it anyway. It should work for most people and in most common hardware/software setups. It's good enough if it's correct in 90 out of 100 cases.
I think that most ordinary users don't want to check their ALSA configuration before launching openttd. That's what auto-detection is designed for - just launching openttd without having to specify any extra parameters. People who want to have full control over the music output can set ALSA_OUTPUT_PORTS.
id10terror
Engineer
Engineer
Posts: 56
Joined: 03 Jul 2009 02:16

Re: ALSA Music/MIDI Driver

Post by id10terror »

I can name a good reason to keep a working midi file reader around. It might be nessary in the future, if i rembember correctly ms has discontinued directmusic. Or if you wanted to move away from directmusic on winxx systems and say toward somthing more open source, it might be quite beneficial.
peter1138
OpenTTD Developer
OpenTTD Developer
Posts: 1740
Joined: 30 Mar 2005 09:43

Re: ALSA Music/MIDI Driver

Post by peter1138 »

Hmm, I wonder how it would look if the file parser part was split off from the main output driver by using an intermediate class?

* MusicDriver_ALSA > MusicDriver_MIDIFile > MusicDriver
* MusicDriver_... > MusicDriver_MIDIFile > MusicDriver
He's like, some kind of OpenTTD developer.
User avatar
bernhard84
Engineer
Engineer
Posts: 5
Joined: 18 Jul 2009 17:47
Location: Austria

Re: ALSA Music/MIDI Driver

Post by bernhard84 »

petern wrote:Hmm, I wonder how it would look if the file parser part was split off from the main output driver by using an intermediate class?
* MusicDriver_ALSA > MusicDriver_MIDIFile > MusicDriver
* MusicDriver_... > MusicDriver_MIDIFile > MusicDriver
Actually not a bad idea. I think I'll implement something like that when I have some time... But it's not gonna be as simple as it seems, because the MIDI parser itself uses ALSA data structures, and I have to get rid of this ALSA dependency somehow.
Post Reply

Return to “OpenTTD Development”

Who is online

Users browsing this forum: No registered users and 3 guests