Mini-Lab: Changing Volume

 


Introduction

The objective of this mini-lab is to practice working with sound in the jes4py library.

Note: Sounds tend to be recorded/saved in many more formats than images. The functions in jes4py that work with sounds are meant to be used with .wav files. Even some of the .wav files you save from the internet may not work properly with our functions in jes4py. It is recommended that you download some sounds from the SoundFiles folder in the MediaSources Directory to use in testing your functions first. Then feel free to test with other sounds, but keep in mind the format of the sounds may not be compatible with our programs.


Changing Volume

  1. In the Volume reading you saw a function that increases the volume of a sound by multiplying the sample values by a factor of 2. What would the factor be if we wanted to decrease the volume by a factor of 2? Would that be the only difference between increasing and decreasing volume?

    Create a new function called changeVolume that allows us to increase or decrease the volume of a sound by any factor. This function should take a sound and a factor as parameters. It should duplicate the sound, and then change the sample values of the new sound appropriately so that the volume changes. At the end of the function, the new sound should be returned.

    Test your new function to see that it can increase or decrease the volume of the sound. Remember to use a .wav file when you select a sound to use. If you need some sounds to pratice with, there are some available in the Media Sources directory.

Variations of Changing Volume

  1. In the Sound Manipulations reading, there is a function called increaseAndDecrease that doubles the volume of the first half of a sound and decreases the volume of the second half of a sound by 40%. Type in this function (or copy and paste it) and test it with several different sounds. Do you hear how the volume changes?

  2. Write a new function called decreaseAndIncrease that decreases the volume by 50% in the first half of a sound, and increases the volume by 50% in the second half of the sound. This function should take a sound as a parameter, should duplicate the sound, make changes to the new sound, and then return the new sound. Think about how this function may be similar/different from the increaseAndDecrease function.
  3. Test this new function with several different sounds. The soundGraph function below takes in a sound and plots the wave form graph. Once the graph is shown, it is saved as a .jpg file. Use this function to obtain a wave form graph of a original sound and a modified sound. Do you see what you would expect?
    
    #Function takes as input a sound and generates a wave form graph for viewing.
    #This graph is saved in the main project folder as a jpg.
    def soundGraph(aSound):
      signal = np.frombuffer(aSound.getBuffer(), dtype ="int16")
    
      # gets the frame rate
      f_rate = aSound.getSamplingRate()
    
      # to Plot the x-axis in seconds
      # you need get the frame rate
      # and divide by size of your signal
      # to create a Time Vector
      # spaced linearly with the size
      # of the audio file
    
      time = np.linspace(
      0, # start
      len(signal) / f_rate,
      num = len(signal)
      )
    
      # using matplotlib to plot
      # creates a new figure
      plt.figure(1)
    
      # title of the plot
      plt.title("Sound Wave")
    
      # label of x-axis
      plt.xlabel("Time")
    
      # actual plotting
      plt.plot(time, signal)
    
      # you can also save
      # the plot using
      pictureName = aSound.getFileName()
      pictureName = pictureName[pictureName.rfind('\\') + 1:pictureName.rfind('.')] + "_Wave_Graph.jpg"
      plt.savefig(pictureName)
    
      # shows the plot
      # in new window
      plt.show()
        
  4. Write a new function called gradualDecrease that decreases the volume of the first third of a sound by 30%, then decreases the volume of the second third of a sound by 60%, and finally decreases the last third of a sound by 90%. Think about what the starting and ending values of your ranges will be.

  5. Test this function with several sounds. Open it using the soundGraph function to see that it is doing what it should be doing.
  6. In the Sound Manipulations reading, there is a function called normalize. This function increases the volume of a sound as much as possible without introducing any clipping. Type in this function and test it with several sounds.

Submit your results

  1. Submit the file you created in this mini-lab via Kit.
  2. Submit a sound and two new sounds that are variations of volume changes on the original sound. Save the sounds the same way you would a picture - modifying the file format .wav instead of .jpg/.png. Include the wave form graph associated with each sound file.