Question: I'm trying to implement 3 types of waves (Triangle, Sawtooth, and Sinusoidal) in Python without using any libraries except time, pygame , array, and math

I'm trying to implement 3 types of waves (Triangle, Sawtooth, and Sinusoidal) in Python without using any libraries except time, pygame, array, and math into the build_samples function

Code that is being used:

import RPi.GPIO as GPIO from time import sleep import pygame from array import array import math

MIXER_FREQ = 44100 MIXER_SIZE = -16 MIXER_CHANS = 1 MIXER_BUFF = 1024

# the note generator class class Note(pygame.mixer.Sound): # note that volume ranges from 0.0 to 1.0 def __init__(self, wavetype, volume): self.frequency = 261.6 self.wavetype = wavetype # initialize the note using an array of samples pygame.mixer.Sound.__init__(self,\ buffer=self.build_samples()) self.set_volume(volume)

# builds an array of samples for the current note def build_samples(self): # calculate the period and amplitude of the note's wave period = int(round(MIXER_FREQ / self.frequency)) amplitude = 2 ** (abs(MIXER_SIZE) - 1) - 1 # initialize the note's samples (using an array of # signed 16-bit "shorts") samples = array("h", [0] * period) # generate the note's samples # square wave if self.wavetype == "square": for t in range(period): if (t

#triangle wave if self.wavetype == "triangle":

return samples

#sawtooth wave if self.wavetype == "sawtooth":

return samples

#sinusoidal wave if self.wavetype == "sinusoidal":

return samples

(Here is a hastebin link providing the code to help with copying/pasting if needed)

https://hastebin.com/xixirolupo.rb

I'm trying to implement 3 types of waves (Triangle, Sawtooth, and Sinusoidal)in Python without using any libraries except time, pygame, array, and mathinto the build_samples function Code that is being used: import RPi.GPIO asGPIO from time import sleep import pygame from array import array importmath MIXER_FREQ = 44100 MIXER_SIZE = -16 MIXER_CHANS = 1 MIXER_BUFF =

An interesting tweak to this activity is to change the shape of the wave for each note as generated by the mixer. That is, keep the frequency and sample rate constant, but change the amplitudes across the waves. This is done in the build_samples function of the Note class: # generate the note's samples for t in range (period) if (t

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!