9.19 If you use the shifting recipe (Program 119) with a factor of 2.0 or 3.0, you’ll get the sound repeated or even triplicated. Why? Can you fix it? Write shiftDur that takes a number of samples (or even seconds) to play the sound.
What will be an ideal response?
You get the sound repeated or triplicated as the higher frequency sound takes up less time, but still has to fill up the full length of the original sound as the function is written. To only make it play once one could either stop copying the original values after one
iteration, or constrain the length of the new sound.
Note: The last sentence of the question could be interpreted in a number of ways. It could be read to indicate that a function should be created that just repeated the sound for a number of seconds, as the below does.
```
def shift2(source, factor, length):
target = makeEmptySound(length)
sourceIndex = 0
for targetIndex in range(0, getLength(target)):
sourceValue = getSampleValueAt(source,int(sourceIndex)) setSampleValueAt(target , targetIndex , sourceValue)
sourceIndex = sourceIndex + factor
if (sourceIndex >= getLength(source)):
sourceIndex = sourceIndex - getLength(source)
play(target)
return target
```
However, the sentence could alternatively be translated to indicate that the new sound should take up the amount of time indicated and that a factor should be determined from these values, such as the below.
```
def shift2(source, length):
target = makeEmptySound(int(length))
sourceIndex = 0
factor = (getLength(source)/length)
for targetIndex in range(0, getLength(target)):
sourceValue = getSampleValueAt(source,int(sourceIndex))
setSampleValueAt(target , targetIndex , sourceValue)
sourceIndex = sourceIndex + factor
if (sourceIndex >= getLength(source)):
sourceIndex = sourceIndex - getLength(source)
play(target)
return target
```
You might also like to view...
Which of the following regulates the use and disclosure of individuals’ personal health information?
A) Sarbanes-Oxley B) HIPAA C) PCI DDS D) GDPR
The symbol >= is a logical operator.
Answer the following statement true (T) or false (F)
A VGA port is a DB-15 female port on a motherboard
Indicate whether the statement is true or false
On a personal computer with Windows Vista Premium, the ipconfig command is entered in the ____________________ window.
What will be an ideal response?