blog |

Shell Scripts, Audio, Images, and 3D Graphics


Yesterday's post gave different views on Shell Scripts As Executable Documentation. I tagged it #shell-the-good-parts and #comments.

This post has the same two tags. It shows examples of a multiprocess architecture for multimedia:

  1. One process generates data in a specified format. This is often the code you write.
  2. Another process renders, plays, or interprets that data. This is often the code you reuse.

I would call this reuse by versionless protocols, as opposed to reuse by fragile libraries. (Related: my comment about textual protocols, which I'll elaborate on in the future.)

Table of Contents
Generators and Renderers
Audio With Raw Samples
Images With netpbm
Raytracing with PBRT Scene Descriptions
The Web Uses This Architecture
Appendix: Raytracing in Pure Bash

Generators and Renderers

Audio With Raw Samples

Summary: you can generate music by piping raw samples into programs like mplayer or ffplay.

Images With netpbm

With the Netpbm file formats, it’s trivial to output pixels using nothing but text based IO

In this case, the protocol / generated format is text rather than binary.

Raytracing with PBRT Scene Descriptions

Are the above techniques silly hacks? Isn't the better solution to use a "proper" library?

No, you can render professional quality computer graphics with this architecture. My 2018 Recurse Center project taught me this:

I wanted to work with graphics rather than text, but I still ended up making use of shell. Why?

  1. The PBRT renderer uses a text file to describe scene graphs. (PBRT is a high quality textbook and C++ implementation of physically-based 3D rendering.)
  2. I wanted to distribute the video frame rendering over 4 multicore machines located in the NYC space. Shell is the easiest way to do this, and it saved us hours of time.


The Web Uses This Architecture

I touched on the relation between Unix and the web in this February 2020 episode of #shell-the-good-parts. Summary:

Now that we've seen some Unix-y examples of audio, images, and 3D graphics, we can add to the analogy:

In a sense, you're reusing the web browser to provide a rich experience to visitors.

Importantly, you can write the web server in any language, which means that this Unix- and web-style of multiprocess programming enables language diversity.

This is good news to designers of nascent languages, e.g. on /r/ProgrammingLanguages! You don't have to write a "binding" to do fun things with a language. You can print data to stdout or write a web server.

Appendix: Raytracing in Pure Bash

An esoteric raytracer in CMake made the rounds recently. I suggested that this program can also be expressed in shell, since shell naturally supports multiple processes for parallel rendering, just as we did with PBRT.

I got a couple great responses!

  1. from Aneesh Durg. A working raytracer in bash. I ran this on my machine and it worked!
  2. The start of another raytracer and two nice articles from Tristan Le Guern:

Important: These pieces of code are different than the ones above! They are more "esoteric" because doing math in shell is slow.

However, I hope to add floating point to the Oil language in the near future, as well as a richer notion of functions. Along with Oil's more familiar syntax, raytracing in shell might not be esoteric!

Ad: I need help on Oil! Just running OSH on existing shell scripts is useful, and there are many other ways to help.