* Stefan Behnel:
Alf P. Steinbach, 12.01.2010 13:10:
* Stefan Behnel:
Maybe you should just stop using the module. Writing the code yourself is certainly going to be faster than reporting that bug, don't you think?

It's part of the standard Python distribution.

Don't you think bugs in the standard library should be fixed?

Sure. I was just emphasizing the fact that it might be a larger contribution to write such a module in the first place than to fix a bug in it.

I'm not interested in credits.

But regarding contributions to the programming community I've done my share, including writings and moderating [comp.lang.c++.moderated]. I do not feel I've done too little and I almost take offense at that suggestion.


The mere fact that the existing module seems valuable enough for you to want to use it suggests that the effort that you are putting into getting your bug fixed in the mainline distribution is worth it.

Well, this is for my Python (actually, beginning programmer) writings, at

  http://tinyurl.com/programmingbookP3

(I hope I spelled that right), which is why it "has" to use standard library functionality only, and why implementing the thing from scratch is not an option -- otherwise, if this was something I needed, I'd probably do that.

It's just to have a concrete real world example of modular arithmetic and two's complement representation.

The example code looks like this, so far -- might of course be of help to others :-)


<code file="simple_wave.py">
"Lets you generate simple mono (single-channel) [.wav] files."
import wave
import array
import math

default_sample_rate     = 44100             # Usual CD quality.

def sample_square( freq, t ):
    linear = freq*t % 1.0
    if linear < 0.5:
        return -1.0
    else:
        return 1.0

def sample_sawtooth( freq, t ):
    linear = freq*t % 1.0
    if linear < 0.5:
        return 4.0*linear - 1.0
    else:
        return 3.0 - 4.0*linear

def sample_sine( freq, t ):
    return math.sin( 2*math.pi*freq*t )

def _append_int_to( a, i ):
    if i < 0:
        i = i + 65536
    assert( 0 <= i < 65536 )
    a.append( i % 256 )
    a.append( i // 256 )

class Writer:
    "Writes samples to a specified [.wav] file"
    def __init__( self, filename, sample_rate = default_sample_rate ):
        self._sample_rate = sample_rate
        self._writer = wave.open( filename, "w" )
        self._writer.setnchannels( 1 )
        self._writer.setsampwidth( 2 )          # 2 bytes = 16 bits
        self._writer.setframerate( sample_rate )
        self._samples = []

    def sample_rate( self ):
        return self._sample_rate

    def write( self, normalized_sample ):
        assert( -1 <= normalized_sample <= +1 )
        self._samples.append( normalized_sample )

    def close( self ):
        data = array.array( "B" )               # B -> unsigned bytes.
        for sample in self._samples:
            level = round( 32767*sample )
            _append_int_to( data, level )
        self._writer.setnframes( len( self._samples ) )
        self._writer.writeframes( data )
        self._writer.close()
</code>


<code file="ringtone.py">
import simple_wave

sample_rate = simple_wave.default_sample_rate
total_time  = 2
n_samples   = sample_rate*total_time

writer = simple_wave.Writer( "test.wav" )
for i in range( n_samples ):
    t = i/sample_rate
    samples = (
        simple_wave.sample_sine( 440, t ),
        simple_wave.sample_sawtooth( (3/2)*440, t ),
        )
    sample = sum( samples )/len( samples )
    writer.write( sample )
writer.close()
</code>


Cheers & hth.,

- Alf
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to