Prototyping 2013-10-07 (Networked Media)

From XPUB & Lens-Based wiki

Construction.gif This page is currently being worked on.

<slidy theme="aa" />

What is a bit?

A little seque into the Binary...

Bit shift

The simple act of opening an audio file in a text editor (or the reverse of opening a text as a digital audio) raises many questions. What exactly is a digital representation of a text, or a sound. The fact that once digital, bits can be as easily interpreted as text or as sound, or image, or other kind of data is at once fascinating, but also deceptively reinforces an idea of multimedia as inherantly bridging and mixing together media. Media formats, and their underlying digital representations, are highly specialized codes (algorithmic, legal) involved in the related processes of encoding and decoding.

A bit shift is the process by which the bits of some data value are shifted in position either "left" or "right" (that is away from or towards the "least significant" bit position). In a simple numerical representation of integers such a shift corresponds to multiplication and division by a power of two. This as a result of the design and working of the system of binary representation, with each column defined to represent powers of 2. The same operation performed on the characters of a text represented as ASCII code values would produce a much different result as the system of representation is structured very differently (with groupings of characters organized not so much by numeric relations, but by clusters of associated symbols and the conventions of the alphabet).

Generating digital media

We can use python to generate the "raw" bits of say an audio waveform or a bitmap image. Certain formats, such as audio WAV files or bitmap formats like TGA and BMP (when uncompressed) are easy to generate and manipulate with generic tools like python because the formats are often mostly "raw" sample or pixel data with a short preceding "header" that declares some key properties about the file (such as sampling rate, or image size).

Bit

Code

  • Notation and Representation
  • Zooming out: Formal Languages: Procedural vs. Descriptive

Generating "Raw" Audio

import wave, struct

filename = "output.wav"
nframes=-1
nchannels=1
sampwidth=2 # in bytes so 2=16bit, 1=8bit
framerate=44100
bufsize=2048

w = wave.open(filename, 'w')
w.setparams((nchannels, sampwidth, framerate, nframes, 'NONE', 'not compressed'))

max_amplitude = float(int((2 ** (sampwidth * 8)) / 2) - 1)

# split the samples into chunks (to reduce memory consumption and improve performance)
#for chunk in grouper(bufsize, samples):
#    frames = ''.join(''.join(struct.pack('h', int(max_amplitude * sample)) for sample in channels) for channels in chunk if channels is not None)
#    w.writeframesraw(frames)

freq = 440
# this means that FREQ times a second, we need to complete a cycle
# there are FRAMERATE samples per second
# so FRAMERATE / FREQ = CYCLE LENGTH
cycle = framerate / freq


data = ''
for i in range(10):
    for x in range(100):
        data += struct.pack('h', int(0.5 * max_amplitude))
    for x in range(100):
        data += struct.pack('h', int(-0.5 * max_amplitude))

w.writeframesraw(data)

w.close()

Generating "Raw" Images

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import struct

f = open("image.data", 'w')

data = ''
for x in range(10000):
    if x % 2:
        data += struct.pack('B', 255)
        data += struct.pack('B', 0)
        data += struct.pack('B', 0)
    else:
        data += struct.pack('B', 0)
        data += struct.pack('B', 255)
        data += struct.pack('B', 0)

f.write(data)
f.close()

Image

File:Cartesian-coordinate-system.svg

A Cartesian coordinate system is a coordinate system that specifies each point uniquely in a plane by a pair of numerical coordinates, which are the signed distances from the point to two fixed perpendicular directed lines, measured in the same unit of length. Each reference line is called a coordinate axis or just axis of the system, and the point where they meet is its origin, usually at ordered pair (0, 0). The coordinates can also be defined as the positions of the perpendicular projections of the point onto the two axes, expressed as signed distances from the origin.

source: Cartesian coordinate system, on wikipedia Raw media

Some binary filters

Bit-flip

Bit-shift

In-class exercise

DSC00087.JPG

Select either to work with Image or Sound.

Use (nested) loops in Python to produce beautiful digital media exploring the bit-friendly nature of Rotterdam.

Resources

  • 10 print book on early BASIC home computer programming culture

Code

  • Code Specifics: Writing a Feed poller / archiver

(Based on an example Max was working on), collecting the results of a "live" feed to make an archive is a nice example of making a custom database / archive from a simple "scraper".

  • Python/Code in different contexts (some examples of "pure" vs embedded vs networked uses)