def violin_example(start=1.2, duration=0.6): """Demonstrates methods in the thinkdsp module. """ # read the violin recording wave = thinkdsp.read_wave('92002__jcveliz__violin-origional.wav') # extract a segment segment = wave.segment(start, duration) # make the spectrum spectrum = segment.make_spectrum() # apply a filter spectrum.low_pass(600) # invert the spectrum filtered = spectrum.make_wave() # prepare the original and filtered segments filtered.normalize() filtered.apodize() segment.apodize() # write the original and filtered segments to a file filename = 'filtered.wav' wfile = thinkdsp.WavFileWriter(filename, segment.framerate) wfile.write(segment) wfile.write(filtered) wfile.close() thinkdsp.play_wave(filename)
def main(): Mountains=TriangleChirp(start=440, end=880) MountianRange=Mountains.make_wave() filename='TriangleChirp' MountianRange.write(filename) dsp.play_wave(filename)
def mix_cosines(): """Demonstrates methods in the thinkdsp module. """ # create a SumSignal cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sig = thinkdsp.SinSignal(freq=880, amp=0.5, offset=0) mix = sin_sig + cos_sig # create a wave wave = mix.make_wave(duration=0.5, start=0, framerate=11025) print 'Number of samples', len(wave.ys) print 'Timestep in ms', 1.0 / 11025 * 1000 # select a segment period = mix.period segment = wave.segment(start=0, duration=period*3) # plot the segment segment.plot() thinkplot.Save(root='example1', xlabel='time (s)', axis=[0, period*3, -1.55, 1.55]) # write the whole wave wave.normalize() wave.apodize() wave.write(filename='example1.wav') # play the wave thinkdsp.play_wave(filename='example1.wav', player='aplay')
def file_example(start=0.1, duration=0.6): """Demonstrates methods in the thinkdsp module. """ # read the file recording wave = thinkdsp.read_wave('51743__erkanozan__applause.wav') # extract a segment segment = wave.segment(start, duration) # make the spectrum spectrum = segment.make_spectrum() # apply a filter spectrum.low_pass(600) # invert the spectrum filtered = spectrum.make_wave() # prepare the original and filtered segments filtered.normalize() filtered.apodize() segment.apodize() # write the original and filtered segments to a file filename = 'filtered.wav' wfile = thinkdsp.WavFileWriter(filename, segment.framerate) wfile.write(segment) wfile.write(filtered) wfile.close() thinkdsp.play_wave(filename)
def main(): print("A Stradavarius In Desperate Straits") # wave = thinkdsp.read_wave('92002__jcveliz__violin-origional.wav') # #thinkdsp.play_wave(filename='92002__jcveliz__violin-origional.wav') # T= 10 #Seawater temp in Celsius # D= 5.556*1/20 #km, 1/20 of a League under the ocean. It seemed appropriate. # Horror=Underwater(wave, T, D) # #Horror.normalize() # Horror.apodize() # Horror.write(filename='DesperateStraits.wav') thinkdsp.play_wave(filename='campfire-1.wav')
def trombone_gliss(): """Tests TromboneGliss. """ low = 262 high = 340 signal = TromboneGliss(high, low) wave1 = signal.make_wave(duration=1) wave1.apodize() signal = TromboneGliss(low, high) wave2 = signal.make_wave(duration=1) wave2.apodize() wave = wave1 | wave2 filename = 'gliss.wav' wave.write(filename) thinkdsp.play_wave(filename) sp = wave.make_spectrogram(1024) sp.plot(high=40) thinkplot.show()
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Aug 2 19:37:36 2018 @author: MikeNagler """ import thinkdsp import numpy as np fundamental = 110 freqs = np.arange(110, 1100, 110) amps = 1/freqs**2 mix = sum(thinkdsp.SinSignal(freq=f, amp=a) for f, a in zip(freqs, amps)) wave = mix.make_wave() segment = wave.segment(duration=0.01) spectrum = wave.make_spectrum() spectrum.plot() wave.write(filename='temp.wav') thinkdsp.play_wave(filename='temp.wav', player='afplay')
wave.plot() decorate(xlabel='Time (s)') # You can write a wave to a WAV file. # In[45]: wave.write('temp.wav') # `wave.write` writes the wave to a file so it can be used by an exernal player. # In[46]: from thinkdsp import play_wave play_wave(filename='temp.wav', player='aplay') # `read_wave` reads WAV files. The WAV examples in the book are from freesound.org. In the contributors section of the book, I list and thank the people who uploaded the sounds I use. # In[49]: from thinkdsp import read_wave wave = read_wave('328878__tzurkan__guitar-phrase-tzu.wav') ##試試其他音效 # In[50]: wave.make_audio()
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Thu Aug 2 17:38:29 2018 @author: MikeNagler """ import thinkdsp from thinkdsp import Signal, Wave, Spectrum tri_sig = thinkdsp.TriangleSignal(freq=440) tri_wave = tri_sig.make_wave(duration=0.01, start=1, framerate=11025) tri_wave.plot() tri_spec = tri_wave.make_spectrum() tri_spec.plot() print(tri_spec.hs[0]) tri_wave.write('temp.wav') thinkdsp.play_wave('temp.wav', 'afplay') tri_spec.hs[0] = 100 tri_spec.plot() tri_wave2 = tri_spec.make_wave() tri_wave2.plot() tri_wave2.write(filename='temp2.wav') thinkdsp.play_wave('temp2.wav', 'afplay')
freqs = np.linspace(self.start, self.end, len(cycle_ts) - 1) dts = np.diff(cycle_ts) dps = thinkdsp.PI2 * freqs * dts phases = np.cumsum(dps) phases = np.insert(phases, 0, 0) ys = np.zeros((num_cycles * len(cycle_ts) + 1)) for i in range(num_cycles): n = i * len(cycle_ts) + 1 print("n=", n) ys[n:n + len(cycle_ts)] = self.amp * np.cos(phases) ys = np.asarray(ys) ys = ys[:(len(ys) - 1)] # ys = thinkdsp.normalize(ys, self.amp) return ys sig = SawtoothChirp() wav = sig.make_wave(duration=3, framerate=11025) #wav.normalize() print(len(wav.ys), "ys", len(wav.ts), "ts") wav.plot() thinkplot.show() wav.write(filename="chirp.wav") thinkdsp.play_wave(filename="chirp.wav", player='vlc')
import thinkdsp from thinkdsp import Wave, Spectrum, Signal def stretch(wave, factor): wave.ts *= factor wave.framerate /= factor """test the function""" wave = thinkdsp.read_wave(filename='guitar_sound.wav') thinkdsp.play_wave(filename='guitar_sound.wav', player='afplay') stretch(wave, 2) wave.write(filename='temp.wav') thinkdsp.play_wave(filename='temp.wav', player='afplay')
cos_sig = thinkdsp.CosSignal(freq=440, amp=1.0, offset=0) sin_sig = thinkdsp.SinSignal(freq=880, amp=0.5, offset=0) # Sum the signals together mix = sin_sig + cos_sig # Evaluate the signal functions into a wave with a specified sample rate wave = mix.make_wave(duration=0.5, start=0, framerate=11025) # Create a new Wave object that contains the first three periods of the Signal object period = mix.period segment = wave.segment(start=0, duration=period * 3) # Plot # segment.plot() # thinkdsp.pyplot.show() # Save the waveform as a Waveform Audio File (WAV) wave.write(filename='output.wav') thinkdsp.play_wave(filename='output.wav', player='wmplayer') # Create audio spectrum as a Spectrum object from the Wave object spectrum = wave.make_spectrum() # Plot # spectrum.plot() # thinkdsp.thinkplot.show() # Wrapper # Read in a .wav file and use it as a Wave object water_wave = thinkdsp.read_wave('RiverStreamAdjusted.wav')
cycle_ts = ts[:round((len(ts)-1)/num_cycles)] freqs = np.linspace(self.start, self.end, len(cycle_ts)-1) dts = np.diff(cycle_ts) dps = thinkdsp.PI2 * freqs * dts phases = np.cumsum(dps) phases = np.insert(phases, 0, 0) ys = np.zeros((num_cycles*len(cycle_ts)+1)) for i in range(num_cycles): n = i*len(cycle_ts)+1 print("n=", n) ys[n:n+len(cycle_ts)] = self.amp * np.cos(phases) ys = np.asarray(ys) ys = ys[:(len(ys)-1)] # ys = thinkdsp.normalize(ys, self.amp) return ys sig = SawtoothChirp() wav = sig.make_wave(duration=3, framerate=11025) #wav.normalize() print(len(wav.ys), "ys", len(wav.ts), "ts") wav.plot() thinkplot.show() wav.write(filename="chirp.wav") thinkdsp.play_wave(filename="chirp.wav", player='vlc')