#---------------------------------------------------------------------- # Program to synthesize sine wave tones (either single or dual) # See page 327 in the Companion # # Copyright (C) April 20, 2018 -- Dr. William T. Verts #---------------------------------------------------------------------- #---------------------------------------------------------------------- # Generate a sine wave sound of one frequency #---------------------------------------------------------------------- def SineWave (Frequency, Seconds=1.0, Amplitude=32767.0, SamplingRate=44100): TotalSamples = int(round(Seconds * SamplingRate)) # {Samples} = {Seconds} * {Samples/Second} MySound = makeEmptySound(TotalSamples, SamplingRate) # Build an empty sound canvas containing the right number of samples RadiansPerSample = (2.0 * math.pi) * Frequency / SamplingRate # {Radians/Sample} = {Radians/Cycle} * {Cycles/Second} / {Samples/Second} for I in range(TotalSamples): Angle = I * RadiansPerSample Value = Amplitude * math.sin(Angle) setSampleValueAt(MySound, I, int(Value)) return MySound #---------------------------------------------------------------------- # Generate a sine wave sound of two mixed frequencies #---------------------------------------------------------------------- def DualSineWave (Frequency1,Frequency2, Seconds=1.0, Amplitude=32767.0, SamplingRate=44100): TotalSamples = int(round(Seconds * SamplingRate)) # Seconds * (samples/second) MySound = makeEmptySound(TotalSamples, SamplingRate) RadiansPerSample1 = (2.0 * math.pi) * Frequency1 / SamplingRate RadiansPerSample2 = (2.0 * math.pi) * Frequency2 / SamplingRate for I in range(TotalSamples): Angle1 = I * RadiansPerSample1 Angle2 = I * RadiansPerSample2 Value1 = Amplitude * math.sin(Angle1) Value2 = Amplitude * math.sin(Angle2) setSampleValueAt(MySound, I, int((Value1+Value2)/2.0)) return MySound #---------------------------------------------------------------------- # Generate a dual tone sound, play it, and save it to a sound file # with a .WAV extension. See page 331 for frequencies to mix to # generate DTMF Touch-Tone(TM) frequencies (playing a DTMF tone into # the handset of a land-line phone dials that digit). #---------------------------------------------------------------------- def Main(): Frequency1 = requestInteger("Enter Frequency 1") Frequency2 = requestInteger("Enter Frequency 2") MySound = DualSineWave(Frequency1,Frequency2) play(MySound) writeSoundTo(MySound,pickAFolder() + "Sound.wav") return