Browse Source

Added some volumes

Graham Northup 10 years ago
parent
commit
960d044246
2 changed files with 4 additions and 3 deletions
  1. 2 2
      broadcast.py
  2. 2 1
      client.py

+ 2 - 2
broadcast.py

@@ -22,7 +22,7 @@ parser.add_option('-q', '--quit', dest='quit', action='store_true', help='Instru
 parser.add_option('-p', '--play', dest='play', action='append', help='Play a single tone or chord (specified multiple times) on all listening clients (either "midi pitch" or "@frequency")')
 parser.add_option('-p', '--play', dest='play', action='append', help='Play a single tone or chord (specified multiple times) on all listening clients (either "midi pitch" or "@frequency")')
 parser.add_option('-P', '--play-async', dest='play_async', action='store_true', help='Don\'t wait for the tone to finish using the local clock')
 parser.add_option('-P', '--play-async', dest='play_async', action='store_true', help='Don\'t wait for the tone to finish using the local clock')
 parser.add_option('-D', '--duration', dest='duration', type='float', help='How long to play this note for')
 parser.add_option('-D', '--duration', dest='duration', type='float', help='How long to play this note for')
-parser.add_option('-V', '--volume', dest='volume', type='int', help='How loud to play this note (0-255)')
+parser.add_option('-V', '--volume', dest='volume', type='int', help='Master volume (0-255)')
 parser.add_option('-s', '--silence', dest='silence', action='store_true', help='Instruct all clients to stop playing any active tones')
 parser.add_option('-s', '--silence', dest='silence', action='store_true', help='Instruct all clients to stop playing any active tones')
 parser.add_option('-S', '--seek', dest='seek', type='float', help='Start time in seconds (scaled by --factor)')
 parser.add_option('-S', '--seek', dest='seek', type='float', help='Start time in seconds (scaled by --factor)')
 parser.add_option('-f', '--factor', dest='factor', type='float', help='Rescale time by this factor (0<f<1 are faster; 0.5 is twice the speed, 2 is half)')
 parser.add_option('-f', '--factor', dest='factor', type='float', help='Rescale time by this factor (0<f<1 are faster; 0.5 is twice the speed, 2 is half)')
@@ -343,7 +343,7 @@ class NSThread(threading.Thread):
 			dur = factor*float(note.get('dur'))
 			dur = factor*float(note.get('dur'))
 			while time.time() - BASETIME < factor*ttime:
 			while time.time() - BASETIME < factor*ttime:
 				self.wait_for(factor*ttime - (time.time() - BASETIME))
 				self.wait_for(factor*ttime - (time.time() - BASETIME))
-			s.sendto(str(Packet(CMD.PLAY, int(dur), int((dur*1000000)%1000000), int(440.0 * 2**((pitch-69)/12.0)), vel * 2 * (options.volume / 255.0))), cl)
+			s.sendto(str(Packet(CMD.PLAY, int(dur), int((dur*1000000)%1000000), int(440.0 * 2**((pitch-69)/12.0)), int(vel*2 * options.volume/255.0))), cl)
                         if options.verbose:
                         if options.verbose:
                             print (time.time() - BASETIME), cl, ': PLAY', pitch, dur, vel
                             print (time.time() - BASETIME), cl, ': PLAY', pitch, dur, vel
 			self.wait_for(dur - ((time.time() - BASETIME) - factor*ttime))
 			self.wait_for(dur - ((time.time() - BASETIME) - factor*ttime))

+ 2 - 1
client.py

@@ -21,6 +21,7 @@ parser.add_option('--generators', dest='generators', action='store_true', help='
 parser.add_option('-u', '--uid', dest='uid', default='', help='Set the UID (identifier) of this client in the network')
 parser.add_option('-u', '--uid', dest='uid', default='', help='Set the UID (identifier) of this client in the network')
 parser.add_option('-p', '--port', dest='port', type='int', default=13676, help='Set the port to listen on')
 parser.add_option('-p', '--port', dest='port', type='int', default=13676, help='Set the port to listen on')
 parser.add_option('-r', '--rate', dest='rate', type='int', default=44100, help='Set the sample rate of the audio device')
 parser.add_option('-r', '--rate', dest='rate', type='int', default=44100, help='Set the sample rate of the audio device')
+parser.add_option('-V', '--volume', dest='volume', type='float', default=1.0, help='Set the volume factor (>1 distorts, <1 attenuates)')
 
 
 options, args = parser.parse_args()
 options, args = parser.parse_args()
 
 
@@ -171,7 +172,7 @@ def samps(freq, phase, cnt):
     global RATE, AMP
     global RATE, AMP
     samps = [0]*cnt
     samps = [0]*cnt
     for i in xrange(cnt):
     for i in xrange(cnt):
-        samps[i] = int(AMP * generator((phase + 2 * math.pi * freq * i / RATE) % (2*math.pi)))
+        samps[i] = int(AMP * max(-1, min(1, options.volume*generator((phase + 2 * math.pi * freq * i / RATE) % (2*math.pi)))))
     return samps, (phase + 2 * math.pi * freq * cnt / RATE) % (2*math.pi)
     return samps, (phase + 2 * math.pi * freq * cnt / RATE) % (2*math.pi)
 
 
 def to_data(samps):
 def to_data(samps):