Posts Tagged ‘open source’

20091102 SyHi: my minimalistic syntax highlighting plug-in for WordPress

I had been using Code Snippet for a year, or a couple of years (I can’t really remember), and was more or less happy with it. Apart from the fact that it didn’t preserve some stuff properly, like double dashes and quotes. So then I tried adding another plug-in to the mix, Preserve Code Formatting. But both at the same time didn’t work out as I expected hoped.

The solution?

I made a new plug-in. It’s very minimalistic, starting with its name: SyHi.

How does it work?

The problem with other plug-ins is that they apply their formatting and then let WordPress continue modifying the posts’ text. That’s a totally bad idea, and that’s why quotes and dashes were systematically altered. On the other hand, this little clever plug-in takes the code blocks apart, sets them aside while leaving placeholder text where they were, and when WordPress has finished with all its filtering and texturizing, SyHi replaces the placeholder texts with the preserved, syntax highlighted beatiful pieces of code that the author entered.

The resulting code not only is nice to look at, but can be copied and pasted into a compiler and it will work with no modifications at all. No more please replace em dashes with double hyphens because WordPress modified the snippet’s output…: your blog readers can now simply copy and paste!

Alphatesters needed

So far I have tested SyHi with a test blog and this blog too. It looks fine, but I would be more than happy if somebody else wants to have a go at testing it. I would even add you to the Thanks section!

Unfortunately, the plug-in is not yet available in the wp-plugins directory; I’m waiting for them to approve my request so that you can install it from within the plugins admin page.

Plug-in page at WordPress directory: SyHi. You can also clone the SyHi github repository or download the latest version from there.

Samples

So you don’t believe me when I say this plug-in works great?

Using Python

noise_output = wave.open('noise.wav', 'w')
noise_output.setparams((2, 2, 44100, 0, 'NONE', 'not compressed'))

for i in range(0, SAMPLE_LEN):
        value = random.randint(-32767, 32767)
        packed_value = struct.pack('h', value)
        noise_output.writeframes(packed_value)
        noise_output.writeframes(packed_value)

noise_output.close()

What about php?

class SyHi
{
        protected $code_blocks;
        protected $geshi_instance = null;

        public function __construct()
        {
                // Execute pre and post process functions before and after each post content is processed
                add_filter('the_content', array(&$this, 'pre_process'), 2);
                add_filter('the_content', array(&$this, 'post_process'), 1000);

                // Same for each comment
                add_filter('comment_text', array(&$this, 'pre_process'), 2);
                add_filter('comment_text', array(&$this, 'post_process'), 1000);

                // Add the css stylesheet link to the <head>
                add_action('wp_head', array(&$this, 'add_css'), 1);
        }
}

OK and what about C/C++?

void luisita_reportErrors(lua_State *L, int st)
{
    if (st != 0)
    {
        std::cerr << "ERROR -- " << lua_tostring(L, -1) << std::endl;
        lua_pop(L, 1); // removes error message
    }
}

As you can see, double dashes are being respected, quotes are kept as they are, and code is nicely highlighted. And if you’re still feeling skeptical, keep browsing posts in this blog, since all code snippets go through SyHi.

Update: added the link to the plug-in’s repository :)

Update 2: let me clarify something: this plug-in can beautify snippets in pretty much every programming language you can think of, thanks to the huge language support provided by GeSHi. I have shown you only three examples as a simple demonstration, but you can even show Z80 assembler code!

The full list of supported languages is in the left column of the GeSHi website, under the Supported Languages header. Heck, it even has support for a language called BrainFuck!

20090420 Generate bitmap font: a little plug-in for The Gimp

I made this little plug-in for the Gimp because I needed to generate a fixed-width font bitmap (what I have called a Bitmap Font) to load and use in an application of mine. I then thought that maybe someone would like to learn how to make plug-ins for the Gimp with Python, as I did, so that’s why I’m posting it here.

It turns out to be very simple. Your plug-ins need to be placed in your user’s directory for the Gimp. In my case, that is ~/.gimp-2.6/plug-ins. When the program starts up, it will scan that folder, looking for any possible plug-ins there.

In my case, I decided I wanted to place the plug-in in the File menu, under Create:

File ... Create...

That is done with the register function at the end of the code, amongst other configuration settings for the window that shows up if you select my plug-in:

register(
        "sole_generate_bitmap_font",
        "Generate bitmap font",
        "Generate bitmap font",
        "Soledad Penades",
        "Soledad Penades",
        "2009",
        "<Image>/File/Create/_Generate Bitmap Font",
        "",
        [
                (PF_FONT, "Font", "Font", "04b03"),
                (PF_SPINNER, "Font_Size", "Size", 8, (0, 1000, 1)),
                (PF_RADIO, "orientation", "Orientation", "horizontal",
                        (("_horizontal", "horizontal"), ("_vertical", "vertical"))
                )
        ],
        [],
        sole_generate_bitmap_font)

Generate bitmap font

The plug-in, as it is, offers only few configuration options but if you want more, they can be added very easily. There’s a but: the only thing I couldn’t manage to do is to remove the “Input image” and “Input drawable” options (note they aren’t specified in my code either, it seems they are added automatically).

Once you press ‘OK’, it will create a new document with the selected font. This is an example:
Example bitmap font

Now, that isn’t a fixed width font. When this texture is used for writing texts, it produces text that can be read but feels weird, because that font has not been designed to be used with fixed widths. Anyway, you get the idea. I’m still looking for a fixed width font that I like and find easy to read, so that’s why I’m using this one for the example. It is the classic 04b_03 by 04.

You can take a look at the full source code here and download it here too, directly from the SVN repository. Enjoy it!

20080804 Neon v2 goes open source

Neon v2

If you’re interested in VJ software, you may probably have heard about Neon v2, the VJ application that shine/xplsv.com developed a few years ago and which has been used not only for lots of VJ gigs but also for creating several real-time demos (including the euskal 2005 winner demo, Sound Pressure)

Unfortunately Shine is very busy right now. But instead of letting the software stagnate, he has made the full sources available under a GPL license, for whoever wants to pick up the development. This pretty much follows the story of Neon v1: when the author (mac/xplsv.com) got too busy, he released the sources for Neon V1 and Shine continued it, creating Neon v2 a while after.

You can check the sources from the SVN repository with your favourite SVN tool, or just be curious and browse them. It has lots of interesting stuff in it. Some examples that come to mind:

  • a full real-time 3d engine, with support for pixel and vertex shaders for building really cool effects
  • 3dsmax mesh export plug-in – if you want to know how to build a plug in for 3dsmax, here’s an example
  • integration with Lua for scripting – allows you to use the engine features programmatically
  • video play, using native windows video support and also ffmpeg for other formats
  • custom skinnable gui

etc etc etc… there are more things but I’ll let you find out by yourself!

As Shine says in the news, if you make new features for it, contact him and we’ll include them on the repository. If the updating gets active we could then manage new users with write-access for the SVN.

I personally wish someone with better knowledge of OpenGL and GUI’s than me would port the software to Mac/Linux (fingers crossed). It would be awesome to have it working without depending on DirectX as it does now (hint hint hint!)

20080612 The making of “Escena.org DVD #1″

Escena.org DVD #1 cover

We finally managed to release the first ever escena.org DVD. It had been the subject of speculations on repeated occasions during the past years and yet it was never done, until now :-)

I’m going to detail how we produced the DVD for two reasons:

  1. Most of the tasks were done with Linux. And although building a DVD with Linux is relatively easy, the information is also slightly hidden and confusing
  2. We would like to see more people involved in producing DVDs and doing stuff in general, specially now that the technology is there and very importantly, is free

The research

First thing I did was to turn to the immediate tools. Since my main computers are macs, running Mac Os, I thought that maybe the famous iDVD, included in iLife, would be all that we needed for creating a couple of clickable still pictures and a series of movies. I couldn’t be more wrong.

In the aim for being easy to use, it’s too limiting. It intently pushes you to all things Apple. Music? From your iTunes database. Pictures? From your iPhoto. And so on and on. It even tries to brand your dvd with an Apple logo on a corner! Yes, you can deactivate it once you find the odd configuration setting, but do you think that’s normal? More annoyances include the inability to edit the provided themes, unless you’re willing to find and edit some property lists hidden somewhere in your ~/Library, or maybe /Library. I am unable to tell exactly where, since no matter how many times I tried to create a new theme, it was never recognized.

A direct consequence of not being able to edit the themes was that you couldn’t change the layout and add, say, three buttons where there was only one. Or push them two pixel up, or down. Apart from the fact that one could easily say that has been done with an Apple product from quite far away, even if you managed to remove the Apple logo.

I investigated a bit more and found that the themes were created with Apple’s Pro application for DVD authoring, DVD Studio Pro. So I thought: OK, probably this iDVD thing is done for people who simply want to torture their friends and family with pictures from their last holidays, I’ll see what can we do with Apple’s Pro stuff.

Enter DVD Studio Pro. Being a professional product, I would expect it to be the swiss army knife of video production… but it couldn’t even load videos which weren’t in MOV format. I don’t want to mess with installing codecs from third parties and etc. Anyway, I installed the missing codecs and tried again. It crashed continuously. When it didn’t crash, it produced odd error messages and when not, it said it was doing stuff and kept saying that for hours. That is, until I forced it to quit. I just couldn’t believe it was so disastrous.

Once I finished with all the immediate possibilities, I decided to go for the exotic ones. I knew there had been some advances in the multimedia field for linux — anyone heard about ubuntu studio, or any of the other multimedia-focused distros? — but I hadn’t had a real chance to check them by myself.

I tried several open source DVD authoring programs. The first one, Q DVD-Author was announced as the definitive full solution for DVD authoring in Linux, and I wouldn’t need to mess with scripts or any command line thing. I thought that it would be cool… if only it worked!

I then tried with DVD Styler, DVD flick, devede… all were promising but none did completely fulfil what we wanted: they weren’t flexible enough or were buggy (like not detecting properly the aspect ratio of the videos and stretching them in a bad way). So I finally investigated a bit more about the technology underneath all these programs and found out that all of them basically are a front end for these external programs:

  • ffmpeg
  • dvdauthor
  • spumux
  • mplex
  • pngtopnm
  • ppmtoy4m
  • mpeg2enc
  • mkisofs

I found a good tutorial on DVD authoring on Linux, which pretty much answered most of the questions I had about the process of converting a series of MPEG files into a DVD, but I still had to find out how to capture the productions in a suitable way and how to convert them to MPEG files. Keep reading…

The workflow

kkapture

Once we agreed on which demos and intros should be featured in the DVD, it was time to capture them. As expected, I used ryg’s kkapture. For getting maximum quality, I used the uncompressed option, which created big (2Gb) fragments of raw AVI with the video, plus a WAV file with the audio. The ideal resolution was 1024×768 for demos without antialias (so they got instant antialias when resized to the DVD dimensions later on). Less detailed demos were fine at 800×600.

Some intros and demos didn’t let themselves to be captured, or when they did, the audio was out of sync in a very uncontrollable manner. Those had to be left out. There were also little quirks with some demos because they do not synch to the music timer (which is what kkapture intercepts, if I understand properly) but to another timer, although they were still passable. (Finding out which ones are slightly wrong is left as an exercise to the viewer!)

Converting to a handier format

Once I the video and audio were captured, it was time to join all the video segments together plus the audio track, using AVIDemux, in order to convert them into a compressed, handier format that I could transfer from the windows box where the captures were done to the linux machine where the DVD authoring process was going to happen. I would have loved to use the raw sources, but that meant a lot of gigabytes per capture.

I first tried to do that using VirtualDub but for some reason it simply choked and refused to load the videos, or refused to export them in the format I wanted, or something else. (I found about AVIDemux in wurstcaptures, an interesting resource about capturing all types of demos).

The captures were compressed to MP4, using h264 for the video (700k, two passes), and the audio was encoded to MP3 using Lame, at the highest quality possible.

Converting did take a bit of time, during which I prepared a script for doing the whole resizing and encoding process automatically.

The script

I didn’t want to resize each video manually, specially since each one needed slightly different parameters for the padding. Also some of them were slightly different internally (having the audio stream first, then the video stream, or the opposite). All that called for a script!

So I would place the input files in a folder (assets/videos), specify the files I wanted to convert in a list in the script, and let it do its magic, which in pseudocode looks like this:

  1. for each video in the playlist:
    1. if it’s not converted to mpeg:
      1. find out the video properties (dimensions and position of the audio and video streams)
      2. calculate the aspect ratio
      3. calculate the final height (using the aspect ratio); make sure it is divisible by two
      4. calculate the amount of vertical padding (above and below the video), if needed
      5. extract the video stream onto a .m2v file (mpeg2 video), while resizing it to the final dimensions and constraining it to dvd frame rate and aspect ratio (25 fps and 4:3 because we were going for a PAL DVD)
      6. extract the audio stream onto a .m2a file, constraining it to dvd requirements (stereo, MP2)
      7. multiplex both files, m2v and m2a

Why would you need to do this? It seems that DVD players aren’t very powerful per se. They just want to be fed a stream which contains packets of video, audio, video, audio, and repeat until the end. This is what is called interleaved data, and that is why we separate the original video in two streams, convert them to the suitable dvd format, and then multiplex them (i.e. mix them together into the easily digestible, interleaved format that DVD players like).

In code, it was something such as this for each video:

# extract the video stream
ffmpeg -i "$src_file" -y -vcodec mpeg2video -s $size_param $padding_param -r 25 -bufsize 1835008 -packetsize 2048 -muxrate 10080000 -aspect 4:3 -minrate 8000k -maxrate 8000k -b 8000k -threads 4 -strict very -f mpeg2video -map $video_stream_number:0 "$tmp_video"

# extract the audio stream
ffmpeg -i "$src_file" -threads 4 -acodec mp2 -ab 256k -ac 2  "$tmp_audio";

# multiplex both together
mplex -f 8 -o "$multiplexed_filename" "$tmp_video" "$tmp_audio"

The menus

Menu screenshot

DVD jargon is slightly confusing for my taste. They speak about menus, titles, titlesets, entry menus, root menus and much more. Confusing, very confusing, specially since it seems that no one really uses entry menus and root, angle or chapter menus at all as they are meant to be used. Or maybe that’s because I have hardly used a real DVD player and only watch them in a computer.

Anyway, I finally found out two things:

  1. the structure we wanted consisted in a main menu (what they call the vmgm menu) which pointed to four sections (i.e four title sets): Productions, Demoscene, Extras and Credits. Furthermore, Productions had three submenus (what they call several menus) for selecting which demo or intro to play, and the demos or intros (the titles), while Demoscene, Extras and Credits simply had a submenu and no titles.
  2. A DVD menu is simply another MPEG 2 file; how you generate it is up to you. Clickable areas are defined with a simple process that is described below

If you have ever inspected a DVD with a file explorer, you probably remember about the VIDEO_TS and AUDIO_TS folders, and the .vob files inside them. That is exactly what dvdauthor generates, once you feed it the already converted to MPEG 2 videos that you want your DVD to include and the structure, defined in an XML file.

The actions for the buttons are specified in this XML. The are some special tags (post tags) which are used for specifying what you want to happen once the playback ends. We wanted the menus to loop so that the background is always moving, and the demos to loop, and when the last demo finishes, it goes back to the first one, non stop, so that you can use the DVD to leave it in loop for example in a festival or something (hint! hint!). There’s more information about this in the tutorial about DVD authoring I mentioned above, with more variations and examples, and of course, in the manual for dvdauthor.

But we still had to generate the menus and specify which areas were clickable somehow. Not having more time for researching, the videos for the menus were assembled with After Effects and then exported as an image sequence (a series of PNG files) which we converted into the desired mpg format by joining them into a m2v file and them multiplexing it with a silence.mp2 (because everything in a DVD needs to have audio, even if it’s a silence), with mplex again.

Silence.mp2 was generated using ffmpeg:

ffmpeg -ar 48000 -t 10 -f s16le -acodec pcm_s16le -i /dev/zero -ab 64K -f mp2 -acodec mp2 -y "$silence_file"

The oddest part of all this is when you find that the clickable areas are defined and mixed into the menu MPEG 2 file using the same tool which is used for adding subtitles to the interleaved .mpg file. This tool is called spumux and again, takes an XML file as input. This XML defines the coordinates of the clickable areas (i.e. the buttons), and also the images which are used for each state of the button.

You need a separate XML file for each menu you want to have, but it doesn’t use a separate image for each button in the menu. Instead, what you do is to create a big image with DVD dimensions (720×576) which contains all the buttons of the menu, and the DVD player will draw only the areas which are described as a button, ignoring the rest. It’s like a mask.

That way you can use different images for creating highlight and rollover effects. But if you inspect in detail the menus of a commercial DVD, you’ll realise that the edges of the highlights are very jagged too. This is a limitation of DVD’s, because the subtitles can’t have more than 4 colours. So wave bye bye to antialias!

In our case since we simply use horizontal underlines, it can’t be noticed, but I got lots of strange errors until I realised that I wasn’t saving the different state images as an 8 bit PNG. Even if it has only 4 or less colours, you can’t use a 24 bit PNG.

Note how we are using an empty image for “image” and “select”, and only use an image for highlighting, so only when you put your mouse over a clickable area, it shows some kind of feedback.

Also, you can’t have non-rectangular buttons. If you see any dvd with non rectangular buttons, it’s just a mask trick.

This step was also added to the script, so that if it didn’t find the mpg menus, it would try to rebuild them from the assets/menu source files. And I had to delete the mpg files lots of times, to force them to be regenerated, until I found the right settings for them.

I had to find a work-around to another limitation: you can’t have a menu without a title. Since the Demoscene, Extras and Credits sections consisted simply in a menu with informative text and a button to go back to the main menu, I had to build an empty title for those menus. I just created an empty PNG file, but 24 bit this time, and multiplexed it with the same silence.mp2 that was created before, and used that empty.mpg as the source for the empty titles.

Create the DVD contents

Once all the movies are converted and the menus generated, it was time to call dvdauthor to obtain the famous AUDIO_TS and VIDEO_TS folders. This was quite slow and the worst of all was that it didn’t check for errors first and then proceed, but instead tried to proceed and check for errors during its process. Quite often there were mistakes at the end of the dvdauthor xml but it didn’t detect them until it had processed all the previous titles, so away went several minutes.

If all went well, we had a nice ./output folder which contained AUDIO_TS and VIDEO_TS.

Extras

Distributing captured demos is cool, but it is cooler if you also include the executable of the demo in the DVD-Rom area of the DVD. So we had an extras folder into which we placed the data (demos, tools, etc) that we wanted to include in the DVD structure.

Build the ISO

Before building the ISO file, we need to copy the extras folder, with all its nice and juicy stuff, to the ./output folder, so that it will appear in the root of the DVD file system when browsing it with a computer.

The ISO itself is generated with a program called mkisofs (make iso file system, I guess). This is part of a suite of tools called cdrtools, and very funnily, the version which comes with Ubuntu and several other distributions is broken because someone decided to make a fork of it and introduced some bugs, apart from making the author get quite angry, I would say. But do not worry!

Nothing that can’t be fixed by downloading the source code and compiling it ourselves. Even more, I was really pleased to see how easy it was to download the whole set of tools, compile them and simply use the tool I wanted (mkisofs), from within the folder I wanted, without having to install them into /usr/local or anything like that. Very neat.

Once I got a new and functioning mkisofs, I was able to generate an ISO image which would work in Mac, Windows and Linux, whereas with the version which came with ubuntu I couldn’t get it to work in Mac and Windows – the DVD appeared to be empty because the filesystem was invisible to them.

Try it yourself!

Here is a tar.gz which contains all the files required for building this DVD. Excepting the video captures, and the sources of the menus, which were pretty huge (being 500 frames each, what did you expect?). Only one frame is included in these cases.

The file structure is pretty immediate if you look at the script for building the DVD – which is done in php so that it could be modified by lots more of people than if it had been done in bash and because well, I don’t like bash scripting too much ;)

So the script is invoked as:

php create.php

You will need to have the required tools installed, which I mostly did with a combination of apt-gets and enabling several repositories. In case of doubt you can always paste the error you got into the magic search genius that will find everything you ask him (google), since most probably someone else had the same problem than you before. Or you can leave a comment and I may help you if I know the answer.

Download the DVD

And if you just want to download the DVD, it’s fine too! Get this torrent and enjoy! (You can also print the cover, designed by the mighty trace of xplsv)

20080228 Open source Flex is great

As I found about the news, I spent some time yesterday having a look at the project’s files. It’s always interesting to have a look at the internals of the software for curious-like-a-cat people like me… but honestly, I’d rather have the Flash player open sourced. That would help to fix, or at least to explain, those absurd issues one finds from time to time when working with Flash.

Even worse: each new version of Flash player introduces slightly different issues. Or bugs, if you prefer. For example, the latest one (9.0.115) has a funky way of playing sound files. If the sample is 44KHz, everything is fine. But if you had a one minute sample with a different rate (say, 22KHz), you can’t simply say go to the second 30 and play, because it will instead go to the second 60 (and hence, finish playing!).

And yes, the issue tracker is now accessible to the general public and so you can directly report these odd behaviours directly to Adobe. See here, here and here for some flash.media.Sound errors already reported.

I’m sure if more people could have a look at the player source, these oddities would be fixed way earlier, or at least they wouldn’t be released to the general public with a major bug like that one introduced so lightly.

It would also make easier to have updated versions of the Flash player in platforms which currently are always a step behind, because they need to wait for Adobe to release them, or do not have support at all, like Linux PowerPC (at least, it hadn’t, last time I tried).

Unfortunately I have the feeling this won’t happen any time soon. With DRM dangerously approaching the path of the player it would be quite surprising to have the source code so that everybody could find work-arounds to those restrictions if they feel like. And it would destroy Adobe’s monopoly too, of course. Which I guess they prefer not to happen.