Question: PYTHON ONLY FOLLOW DIRECIONS GIVEN! ring_buffer.py Models a ring buffer. import stdarray import stdio import sys def create(capacity): Create and return a
PYTHON ONLY FOLLOW DIRECIONS GIVEN!
""" ring_buffer.py
Models a ring buffer. """
import stdarray import stdio import sys
def create(capacity): """ Create and return a ring buffer, with the given maximum capacity and with all elements initialized to None. A ring buffer is represented as a list of four elements: the buffer (buff) itself as a list; number of elements (size) currently in buff; the index (first) of the least recently inserted item; and the index (last) one beyond the most recently inserted item. """
...
def capacity(rb): """ Return the capacity of the ring buffer. """
...
def size(rb): """ Return the number of items currently in the buffer rb. """
...
def is_empty(rb): """ Return True if the buffer rb is empty and False otherwise. """
...
def is_full(rb): """ Return True if the buffer rb is full and False otherwise. """
...
def enqueue(rb, x): """ Add item x to the end of the buffer rb. """
...
def dequeue(rb): """ Delete and return item from the front of the buffer rb. """
...
def peek(rb): """ Return (but do not delete) item from the front of the buffer rb. """
...
def _main(): """ Test client [DO NOT EDIT]. """
N = int(sys.argv[1]) rb = create(N) for i in range(1, N + 1): enqueue(rb, i) t = dequeue(rb) enqueue(rb, t) stdio.writef('Size after wrap-around is %d ', size(rb)) while size(rb) >= 2: x = dequeue(rb) y = dequeue(rb) enqueue(rb, x + y) stdio.writeln(peek(rb))
if __name__ == '__main__': _main()
The purpose of this project is to write a program to simulate the plucking of a guitar string using the Rarples-Strong algorithm. This algorithm played a seminal role in the emergence of physically modeled sound symthesis, where a physical description of a musical instrument is used to synthesize sound electronically. Simulate the Plucking of a Guitar String When a guitar string is plucked, the string vibrates and creates sound. The length of the string deteruuinees its fundamental frequency of vibration. We model a guitar string by pling its displacement mber between -1/2 and +1/2) at N equally spaced points uals the The integer N eq pling nate (44,100 Hz) di ded by the desired fundamental fr rounded up to the next integer. equency eg a String The of the string y frequency. mulate the excitation white noise: set each of the N displacements to a random real ber between -1/2 and +1/2. The Resulting After the string is plucked, the string vibrates. The pluck causes a displacement that spreads The Karplus-Strong algorith ing buffer of the N brati ple from the buffer and adds to the cnd of the buffer the average samples: the algorithm repeatedl y deletes the first of the deleted sample and the first sample led by an energy decay factor of 0.996. For example time t 2988 me t+ The two primary components that make the Karplus-Strong algorithm work are the ring buffer feedback why it wo mechanism and the averaging operation. The Ring Buffer Feedback Mech The builer models the medi tied down at both ends tring the energy travels back and forth. The length of the ring buffer determines the fundamental frequency of the resulting sound. Sonically, the feedback mechanism reinforces only the fundamental frequency and its harmonics (frequencies integer multiples of the fundamental The energy decay Eactor -996 in this case) models the slight dissipation in energy as the wave makes a round trip through the string The A ging operati gentle which higher freq aging operati while allowing lower frequencies to pass, hence the name. Because it is in the path of the feedback, this has the effect of gradually attenuating the higher harmonics while keeping the lower ones, which corresponds closely with how a plucked Froua a heamatical physics viewpolat. the Karplus-strong algorithm appeoximately solves the ID wave equation whieh deseribes the trauswerse tion of the string Problem 1. (Ring Bater) Y first task is to model the bufer. Write a wodule rink. that implements the following AP all elements initialized to capacity of the buffer number of it ntly in the buffer rb is the buffer rb empty? returm (beat do not delete) item from the front of the benf5er rb Simre the ring bmfler has list (tar floats of that length, with the narmber of elements he buffer stored For efficiency, coche array-around, which that each oper doua unt of time. W that atoes the ladex of the least inseated item, and put it 0. The ist of four element he bater (hurt): mber of the ind of the least recently inserted item: and the index beyond the most ly inserted item. For example, the rimk buffer shown im the figure below the list built capacity 10 Calling full buffer ahnukl th the mm-age "Error full Calling empty buffer should terminate the progr th the mereasse "Error cammot Peek not derametre empty buffer". Use sys the mersage nse. to terminate
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
