Nooooootes: Difference between revisions

From XPUB & Lens-Based wiki
(Replaced content with "20131017:raw sound/image")
Line 1: Line 1:
== BIT ==
20131017:raw sound/image
binary 二进制的,二元的 digit 数字
Binary digit bit is general tool of python to open files,count...
== 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).
 
== Raw 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).
 
==generate sound by python ==
<source lang="python">
import wave, struct
 
filename = "output.wav"
nframes=0
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()
</source>
 
== Generating "Raw" Images ==
{{:Raw image}}

Revision as of 12:06, 14 October 2013

20131017:raw sound/image