'Volume control in Pyaudio

I have pilfered some of the following code from previous stack exchange posts about creating simple sine wave tones with pyaudio. The code is written to loop through the notes of the chromatic scale over two octaves. Each note has a frequency selected from the array named "notes". My questions: Why is the volume changing with each note? How can I keep the volume the same for each note? Please know that I am a pyaudio beginner.

import pyaudio
import numpy as np
import time
from numpy import zeros

p = pyaudio.PyAudio()

SR = 44100       # sampling rate
duration = 2.0   # seconds
durationPause = 2.0

#Frequency of pitches
notes = [220.00, 233.08, 246.94, 261.63, 277.18, 293.66, 311.13, 329.63, 349.23, 369.99, 392.00, 415.30, 440.00, 466.16, 493.88, 523.25, 554.37, 587.33, 622.25, 659.25, 698.46, 739.99, 783.99, 830.61, 880.00]

stream = p.open(format=pyaudio.paFloat32,
                channels=1,
                rate=SR,
                output=True)

count = 0
while count < 23:
  count += 1
  f = notes[count]
  #create sine wave of frequency f
  samples = (np.sin(2.0*np.pi*np.arange(int(SR*duration))*(f/SR))).astype(np.float32) 
  #create pause
  pause = zeros(int(SR*durationPause)).astype(np.float32)
  #concatenate pitch and pause
  samples = np.concatenate((samples, pause))
  # Play sound.  
  stream.write((samples).tobytes())
  

stream.stop_stream()
stream.close()

p.terminate()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source