@@ -0,0 +1,1988 @@
+# Play a wave file with amplitude modulation. %0A# Assumes wave file is mono.%0A# This implementation reads and plays a block at a time (blocking).%0A%0Aimport pyaudio%0Aimport struct%0Aimport wave%0Aimport math%0A%0A# f0 = 0      # Normal audio%0Af0 = 400    # 'Duck' audio%0A%0ABLOCKSIZE = 64      # Number of frames per block%0A%0A# Open wave file (mono)%0Ainput_wavefile = 'author.wav'%0A%0Awf = wave.open( input_wavefile, 'rb')%0ARATE = wf.getframerate()%0AWIDTH = wf.getsampwidth()%0ALEN = wf.getnframes() %0ACHANNELS = wf.getnchannels() %0A%0Aprint 'The sampling rate is %7B0:d%7D samples per second'.format(RATE)%0Aprint 'Each sample is %7B0:d%7D bytes'.format(WIDTH)%0Aprint 'The signal is %7B0:d%7D samples long'.format(LEN)%0Aprint 'The signal has %7B0:d%7D channel(s)'.format(CHANNELS)%0A%0A# Open audio stream%0Ap = pyaudio.PyAudio()%0Astream = p.open(format = p.get_format_from_width(WIDTH),%0A                channels = 1,%0A                rate = RATE,%0A                input = False,%0A                output = True)%0A%0A# Create block (initialize to zero)%0Aoutput_block = %5B0 for n in range(0, BLOCKSIZE)%5D%0A%0A# Number of blocks in wave file%0Anum_blocks = int(math.floor(LEN/BLOCKSIZE))%0A%0Aprint('* Playing...')%0A%0A# Go through wave file %0Afor i in range(0, num_blocks):%0A%0A    # Get block of samples from wave file%0A    input_string = wf.readframes(BLOCKSIZE)     # BLOCKSIZE = number of frames read%0A%0A    # Convert binary string to tuple of numbers    %0A    input_tuple = struct.unpack('h' * BLOCKSIZE, input_string)%0A            # (h: two bytes per sample (WIDTH = 2))%0A%0A    # Go through block%0A    for n in range(0, BLOCKSIZE):%0A        # Amplitude modulation  (f0 Hz cosine)%0A        output_block%5Bn%5D = input_tuple%5Bn%5D * math.cos(2*math.pi*n*f0/RATE)%0A        # output_block%5Bn%5D = input_tuple%5Bn%5D * 1.0  # for no processing%0A%0A    # Convert values to binary string%0A    output_string = struct.pack('h' * BLOCKSIZE, *output_block)%0A%0A    # Write binary string to audio output stream%0A    stream.write(output_string)%0A%0Aprint('* Done')%0A%0Astream.stop_stream()%0Astream.close()%0Ap.terminate()%0A

Create blocking.py