#********************************************************************** # Program to synthesize audio waveforms from one or two sine waves. # # Copyright (C) December 4, 2017 -- Dr. William T. Verts # # See pages 327-331 in the Companion #********************************************************************** import time #---------------------------------------------------------------------- # Synthesize a pure tone from one sine wave. #---------------------------------------------------------------------- def SineWave (Frequency, Seconds=1.0, Amplitude=32767.0, SamplingRate=44100): TotalSamples = int(round(Seconds * SamplingRate)) MySound = makeEmptySound(TotalSamples, SamplingRate) RadiansPerSample = (2.0*math.pi) * Frequency / SamplingRate for I in range(TotalSamples): Angle = I * RadiansPerSample Value = Amplitude * math.sin(Angle) setSampleValueAt(MySound, I, int(Value)) return MySound #---------------------------------------------------------------------- # Synthesize a tone from the average of two sine waves. # DTMF (Touch-tone telephone sounds) use this with frequencies from # the table on page 331. For example, a Touch-tone "0" is composed of # a blend of 941 Hz and 1336 Hz sine waves. #---------------------------------------------------------------------- def DualSineWave (Frequency1, Frequency2, Seconds=1.0, Amplitude=32767.0, SamplingRate=44100): TotalSamples = int(round(Seconds * SamplingRate)) 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) Value = (Value1 + Value2) / 2.0 setSampleValueAt(MySound, I, int(Value)) return MySound #---------------------------------------------------------------------- # Get a phone number from the user and play it. Playing the resulting # sounds into the mouthpiece of a touch-tone telephone will dial the # entered number. #---------------------------------------------------------------------- def Main(): DTMF = {"1":[697,1209], "2":[697,1336], "3":[697,1447], \ "4":[770,1209], "5":[770,1336], "6":[770,1447], \ "7":[852,1209], "8":[852,1336], "9":[852,1447], \ "*":[941,1209], "0":[941,1336], "#":[941,1447]} S = requestString("Enter a phone number") for CH in S: if CH in DTMF: Frequencies = DTMF[CH] F1 = Frequencies[0] F2 = Frequencies[1] blockingPlay(DualSineWave(F1, F2, 0.35)) # Make sure one sound stops before next one plays time.sleep(0.1) return