python - Why is my output disregarding every other sample and why is the frequency half of the expected value? -


i'm playing python's wave module, , have run 2 issues can't figure out.

  1. every other sample has amplitude of 0. desired behavior have continuous sinusoidal wave.

  2. the produced frequency seems half of desired frequency. looking @ math, can't figure why. understand how fix it. can't figure out reasoning. in mind, multiplying frequency 2π, not 4π, makes sense me.

    from functools import partial, wraps math import pi, sin import struct import wave  class wave:     def __init__(self, frequency, amplitude=1.0, phase_shift=0.0, vertical_translation=0.0):         self.frequency = frequency         self.amplitude = amplitude         self.phase_shift = phase_shift         self.vertical_translation = vertical_translation      def __call__(self, time):         try:             amplitude = self.amplitude(time)         except typeerror:             amplitude = self.amplitude          try:             frequency = self.frequency(time)         except typeerror:             frequency = self.frequency          try:             phase_shift = self.phase_shift(time)         except typeerror:             phase_shift = self.phase_shift          try:             vertical_translation = self.vertical_translation(time)         except typeerror:             vertical_translation = self.vertical_translation          return amplitude * sin(2 * pi * frequency * time + phase_shift) + vertical_translation  if __name__ == '__main__':     sample_rate = 96000     number_of_channels = 1     sample_width = 4     max_amplitude = 2 ** (8 * sample_width - 1)      wave.open('output.wav', 'w') output:         output.setsampwidth(sample_width)         output.setnchannels(number_of_channels)         output.setframerate(sample_rate)         output.setcomptype('none', 'uncompressed')          a440 = wave(440)          time in range(sample_rate // a440.frequency):             sample = max_amplitude * a440(time / sample_rate)             output.writeframes(struct.pack('l', round(sample))) 

i'd appreciate insight.

i can not reproduce issue. after running code perfect sine wave of expected length (1/440hz ≈ 0.0023s) when inspecting .wav in audacity:

sine wave

i have 1 note you, can't round whatever comes out of sin * max_amplitude. have truncate towards negative infinity. because (say) 8-bit sample goes -128 127, not -128 128.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -