1.20131007 Image or Sound-generating Loop: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "For next week, please work on getting either an image or sound- generating loop to work. The code requires no external libraries, and for this particular exercise please work ...")
 
No edit summary
 
Line 1: Line 1:
For next week, please work on getting either an image or sound-
==1.吡————!==
generating loop to work. The code requires no external libraries, and
<source lang = 'python'>
for this particular exercise please work within this limitation, ie for
  import wave , struct
now we don't consider loading any "external" data yet. See it as an
 
exercise in working within the limitations (your own and of the code).
  filename = "mmm.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)
 
  freq = 220
 
  cycle = framerate / freq
  data = ''
  for i in range(20):
      for x in range(200):
 
 
          data += struct.pack('h', int(0.1 * max_amplitude))
      for x in range(200):
          data += struct.pack('h', int(-0.1 * max_amplitude))
  w.writeframesraw(data)
  w.close()
</source>
[[File:Mmm-1.ogg]]
 
==2.failed :only generate a silent wav file.==
<source lang = 'python'>
import wave, struct
  import struct from math import sin,pi,pow
  max_amplitude = 32767
  sample_rate = 44100
  duration_sec = 3
  sample_len = sample_rate*duration_sec
  filename = 'hei.wav'
  print "creating sound file:",filename
  print "sample rate:",sample_rate
  print "duration(sec):",duration_sec
  print "# samples",sample_len
  wavefile = wave.open(filename, 'w')
  wavefile.setparams((2,2,sample_rate,0,'none','not compressed'))
  samples = []
  for i in range(sample_len):
        t = float(i) / sample_rate
        sample = max_amplitude*sin(t*320*2*pi)
      #print i, t, sample
      packed_sample = struct.packed_sample('h', sample)
      samples.append(packed_sample)
      samples.append(packed_sample)
  sample_str = ''.join(samples)
  wavefile.writeframes(sample_str)
  wavefile.close()
  print "done writing file."
</source>

Latest revision as of 23:52, 13 October 2013

1.吡————!

  import wave , struct

  filename = "mmm.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)

 
  freq = 220

  cycle = framerate / freq
 
 
  data = ''
  for i in range(20):
      for x in range(200):


          data += struct.pack('h', int(0.1 * max_amplitude))
      for x in range(200):
          data += struct.pack('h', int(-0.1 * max_amplitude))
 
  w.writeframesraw(data)
 
  w.close()

File:Mmm-1.ogg

2.failed :only generate a silent wav file.

import wave, struct
  import struct from math import sin,pi,pow
 
  max_amplitude = 32767
  sample_rate = 44100
  duration_sec = 3
  sample_len = sample_rate*duration_sec
  filename = 'hei.wav'
  print "creating sound file:",filename
  print "sample rate:",sample_rate
  print "duration(sec):",duration_sec
  print "# samples",sample_len
  wavefile = wave.open(filename, 'w')
  wavefile.setparams((2,2,sample_rate,0,'none','not compressed'))
  samples = []
 
  for i in range(sample_len):
        t = float(i) / sample_rate
        sample = max_amplitude*sin(t*320*2*pi)
 
       #print i, t, sample
       packed_sample = struct.packed_sample('h', sample)
       samples.append(packed_sample)
       samples.append(packed_sample)
 
  sample_str = ''.join(samples)	
  wavefile.writeframes(sample_str)
  wavefile.close()
  print "done writing file."