what is the biggest number that i can send to Wave_write.writeframes(data)

Rob Williscroft rtw at freenet.co.uk
Tue Jun 2 18:13:59 EDT 2009


'2+ wrote in news:mailman.1017.1243932401.8015.python-list at python.org in 
comp.lang.python:

> would like to take advantage of the wave module
> found a good example here:
> http://www.python-forum.org/pythonforum/viewtopic.php?f=2&t=10644
> 
> hmm .. i don't get how to write a stereo .. i mean i can set nchannels
> .. but how do i actually take control of each ch individually?

Interleave the channels, one sample for the left then one sample 
for the right (or maybe its the other way around).

> and what's the range(in float) of the data i can set in

The range of a signed 16 bit int, -2**15 to 2**15 - 1.

> wav_file.writeframes(struct.pack('h', data))?

Example:

import wave
from StringIO import StringIO

out = StringIO()

AMPLITUDE = 2 ** 15

w = wave.open( out, "w" )
w.setnchannels( 2 )
w.setsampwidth( 2 ) #BYTES
w.setframerate( 22000 )

from array import array
import math

F = 261.626
F2 = F * (2 ** (5 / 12.))
ang = 0.0
ang2 = 0.0
delta = ( math.pi * 2 * F  ) / 22000.0
delta2 = ( math.pi * 2 * F2  ) / 22000.0

for cycle in xrange( 4 ):
  data = array( 'h' )
  for pos in xrange( 22000 ):
    amp = AMPLITUDE * (pos / 22000.0)
    amp2 = AMPLITUDE - amp
    if cycle & 1:
      amp, amp2 = amp2, amp
      
    data.append( int( ( amp * math.sin( ang ) ) ) )
    data.append( int( ( amp2 * math.sin( ang2 ) ) ) )
    
    ang += delta
    ang2 += delta2
  
  w.writeframes( data.tostring() )
  
w.close()

sample = out.getvalue()
out.close()

#a wx player

import wx

app = wx.PySimpleApp()

class Frame( wx.Dialog ):
  def __init__( self, *args ):
    wx.Dialog.__init__( self, *args )
    b = wx.Button( self, -1, "Ok" )
    b.Bind( wx.EVT_BUTTON, self.button )
  
  def button( self, event ):
    self.sound = wx.SoundFromData( sample ) 
    self.sound.Play( wx.SOUND_ASYNC )

frame = Frame( None )
frame.Show()

app.MainLoop()

Rob.
-- 
http://www.victim-prime.dsl.pipex.com/



More information about the Python-list mailing list