{"id":3381,"date":"2011-05-31T07:02:15","date_gmt":"2011-05-31T14:02:15","guid":{"rendered":"http:\/\/multimedia.cx\/eggs\/?p=3381"},"modified":"2011-12-29T10:01:45","modified_gmt":"2011-12-29T18:01:45","slug":"salty-game-music","status":"publish","type":"post","link":"https:\/\/multimedia.cx\/eggs\/salty-game-music\/","title":{"rendered":"Salty Game Music"},"content":{"rendered":"<p>Have you heard of <a href=\"http:\/\/code.google.com\/p\/nativeclient\/\">Google&#8217;s Native Client (NaCl) project<\/a>? Probably not. Basically, it allows native code modules to run inside a browser (where &#8216;browser&#8217; is defined pretty narrowly as &#8216;Google Chrome&#8217; in this case). Programs are sandboxed so they aren&#8217;t a security menace (or so the whitepapers claim) but are allowed to access a variety of APIs including video and audio. The latter API is significant because sound tends to be forgotten in all the hullabaloo surrounding non-Flash web technologies. At any rate, enjoy NaCl while you can because I suspect it won&#8217;t be around much longer.<\/p>\n<p>After <a href=\"http:\/\/multimedia.cx\/eggs\/nosefart-and-gme\/\">my recent work upgrading some old music synthesis programs<\/a> to use more modern audio APIs, I got the idea to try porting the same code to run under NaCl in Chrome (first Nosefart, then Game Music Emu\/GME). In this exercise, I met with very limited success. This blog post documents some of the pitfalls in my excursion.<\/p>\n<p><center><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2011\/05\/saltygme-snapshot.png\" alt=\"\" title=\"Screenshot for SaltyGME prototype\" width=\"674\" height=\"245\" class=\"aligncenter size-full wp-image-3382\" srcset=\"https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2011\/05\/saltygme-snapshot.png 674w, https:\/\/multimedia.cx\/eggs\/wp-content\/uploads\/2011\/05\/saltygme-snapshot-300x109.png 300w\" sizes=\"auto, (max-width: 674px) 100vw, 674px\" \/><br \/>\n<\/center><\/p>\n<p><strong>Infrastructure<\/strong><br \/>\nPeople who know me know that I&#8217;m rather partial &#8212; to put it gently &#8212; to straight-up C vs. C++. The NaCl SDK is heavily skewed towards C++. However, it does provide a Python tool called init_project.py which can create the skeleton of a project and can do so in C with the <code>'-c'<\/code> option:<\/p>\n<pre>\r\n.\/init_project.py -c -n saltynosefart\r\n<\/pre>\n<p>This generates something that can be built using a simple &#8216;make&#8217;. When I added Nosefart&#8217;s C files, I learned that the project Makefile has places for project-necessary CFLAGS but does not honor them. The problem is that the generated Makefile includes a broader system Makefile that overrides the CFLAGS in the project Makefile. Going into the system Makefile and changing <code>\"CFLAGS =\"<\/code> -&gt; <code>\"CFLAGS +=\"<\/code> solves this problem.<\/p>\n<p>Still, maybe I&#8217;m the first person to attempt building something in Native Client so I&#8217;m the first person to notice this?<\/p>\n<p><strong>Basic Playback<\/strong><br \/>\n<a href=\"http:\/\/code.google.com\/chrome\/nativeclient\/docs\/audio.html\">At least the process to create an audio-enabled NaCl app is well-documented.<\/a> Too bad it doesn&#8217;t seem to compile as advertised. According to my notes on the matter, I filled in <code>PPP_InitializeModule()<\/code> with the appropriate boilerplate as outlined in the docs but got a linker error concerning get_browser_interface().<\/p>\n<p><strong>Plan B: C++<\/strong><br \/>\nObviously, the straight C stuff is very much a second-class citizen in this NaCl setup. Fortunately, there is already that fully functional tone generator example program in the limited samples suite. Plan B is to copy that project and edit it until it accepts Nosefart\/GME audio instead of a sine wave.<\/p>\n<p>The build system assumes all C++ files should have .cc extensions. I have to make some fixes so that it will accept .cpp files (either that, or rename all .cpp to .cc, but that&#8217;s not very clean).<\/p>\n<p><strong>Making Noise<\/strong><br \/>\nYou&#8217;ll be happy to know that I did successfully swap out the tone generator for either Nosefart or GME. Nosefart has a slightly fickle API that requires revving the emulator frame by frame and generating a certain number of audio samples. GME&#8217;s API is much easier to work with in this situation &#8212; just tell it how many samples it needs to generate and give it a pointer to a buffer. I played NES and SNES music play through this ad-hoc browser plugin, and I&#8217;m confident all the other supported formats would have worked if I went through the bother of converting the music data files into C headers to be included in the NaCl executable binaries (dynamically loading data via the network promised to be a far more challenging prospect reserved for phase 3 of the project).<\/p>\n<p><strong>Portable?<\/strong><br \/>\nI wouldn&#8217;t say so. I developed it on Linux and things ran fine there. I tried to run the same binaries on the Windows version of Chrome to no avail. It looks like it wasn&#8217;t even loading the .nexe files (NaCl executables).<\/p>\n<p><strong>Thinking About The (Lack Of A) Future<\/strong><br \/>\nAs I was working on this project, I noticed that the online NaCl documentation materialized explicit banners warning that my NaCl binaries compiled for Chrome 11 won&#8217;t work for Chrome 12 and that I need to code to the newly-released 0.3 SDK version. Not a fuzzy feeling. I also don&#8217;t feel good that I&#8217;m working from examples using bleeding edge APIs that feature deprecation as part of their naming convention, e.g., pp::deprecated::ScriptableObject().<\/p>\n<p>Ever-changing API <strong>+<\/strong> minimal API documentation + API that only works in one browser brand <strong>+<\/strong> requiring end user to explicitly enable feature = &#8230; well, that&#8217;s why I didn&#8217;t bother to release any showcase pertaining to this little experiment. Would have been neat, but I strongly suspect that this is yet another one of <a href=\"http:\/\/googlecode.blogspot.com\/2011\/05\/spring-cleaning-for-some-of-our-apis.html\">those APIs that Google decides to deprecate<\/a> soon.<\/p>\n<p><strong>See Also:<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/multimedia.cx\/eggs\/back-on-the-salty-track\/\">I eventually got a second wind and made this work using the C interface.<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Adventures in using Google Chrome&#8217;s upcoming Native Client (NaCl) SDK to port audio synthesis libraries to run inside a browser<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3381","post","type-post","status-publish","format-standard","hentry","category-general"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/3381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/comments?post=3381"}],"version-history":[{"count":12,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/3381\/revisions"}],"predecessor-version":[{"id":3679,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/posts\/3381\/revisions\/3679"}],"wp:attachment":[{"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/media?parent=3381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/categories?post=3381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multimedia.cx\/eggs\/wp-json\/wp\/v2\/tags?post=3381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}