Esempio n. 1
0
 def capture_1sec(self):
     # duration = 1 sec hence   1 x self.audio_sampling_rate = self.audio_sampling_rate
     one_sec_record = b''
     try:
         one_sec_record = sounddevice.rec(self.audio_sampling_rate)
     except sounddevice.PortAudioError as err:
         print("Error reading device", self.name)
         print(err)
     return one_sec_record[0]
Esempio n. 2
0
def rec(fs, fn):
    sd.default.samplerate = fs
    sd.default.dtype = "int16"  # fixme: uint?
    sd.default.channels = 1

    duration = 10
    X = sd.rec(duration * fs)
    X = (X.T)[0]

    sd.wait()

    write("e.wav", fs, X)
Esempio n. 3
0
def run_test(time):
    duration = time # seconds
    fs = 48000
    sd.default.samplerate = fs
    audio = sd.rec(duration * fs, channels=2, dtype="float64")
    sd.wait()

    combs = {}
    for pair in combinations(range(audio.shape[1]), 2):
        if not pair[0] in combs:
            combs[pair[0]] = {}
        shift = _compute_shift(audio[:, pair[1]], audio[:, pair[0]])
        combs[pair[0]][pair[1]] = shift / (fs / 1000)
    return combs
Esempio n. 4
0
def offline():
    print("Offline analysis")
    duration = 5 # seconds
    fs = 48000
    sd.default.samplerate = fs
    audio = sd.rec(duration * fs, channels=2, dtype="float64")
    sd.wait()

    print("  [{} channels, {:.2f} sec. {} Hz]".format(audio.shape[1], audio.shape[0] / fs, fs))
    for pair in combinations(range(audio.shape[1]), 2):
        print("  Ch. {} -> {}:".format(*pair), end='')
        shift = _compute_shift(audio[:, pair[1]], audio[:, pair[0]])
        print(" {:>5} sample(s) [{:>7,.3f} ms]".format(shift, shift / (fs / 1000)))

    sd.play(audio)
    sd.wait()
Esempio n. 5
0
def audio_sample_generator(duration=10, sample_rate=2000):
    """
    Yields a series of audio recordings from the system microphone.

    Args:
        duration (float): Time in seconds for each recording.
        sample_rate (float): Sample rate in Hz.

    Yields:
        numpy array: 1-d numpy array of audio data as int16 samples.

    """
    samples = int(duration * sample_rate)
    while True:
        recording = sounddevice.rec(samples, samplerate=sample_rate,
                                    channels=1, dtype='int16')
        sounddevice.wait()
        yield recording[:, 0]
Esempio n. 6
0
def record_sample(duration=3,samplerate=22050,channels=1,blocking=False):
    """
    An optionally blocking method used to start the recording of a sample.

    Parameters:
    -----------
    duration : int
        The length in seconds to record
    samplerate : int
        The number of samplesa per second to record at
    channels : int
        The number of channels to be used during recording
    blocking : bool
        True if method should block until it completes. False otherwise.
    """
    sample=sd.rec(duration * samplerate, samplerate=samplerate, channels=channels, dtype='int16')
    if blocking == True:
        sd.wait()
    return sample
Esempio n. 7
0
def check_play_audio():
    print("")
    print("Recording Audio")
    print("===============")
    print("")
    print(
        "This test checks that we can record audio and that you can hear the recording."
    )

    while True:
        print("")

        device_strings = sd.query_devices()
        default_input_index = sd.default.device[0]
        default_device_string = device_strings[default_input_index]["name"]
        print("Selected input device:", default_device_string)

        max_input_channels = device_strings[default_input_index][
            "max_input_channels"]
        print("Maximum number of input channels:", max_input_channels)

        print(
            "\nWhen you are ready to record, press enter.  Recording will start immediately"
        )
        print("and last for three seconds.")
        user_input = input()

        print("\nRecording for three seconds...")
        duration = 3  # seconds
        samples_per_second = 48000
        samples_to_record = duration * samples_per_second
        channels = max_input_channels
        audio = sd.rec(samples_to_record,
                       samplerate=samples_per_second,
                       channels=channels)
        sd.wait()

        print("")

        default_output_index = sd.default.device[1]
        default_device_string = device_strings[default_output_index]["name"]
        print("Selected output device:", default_device_string)

        print("Playing back recording...")
        sd.play(audio, samples_per_second)

        print("\nCan you hear your recording?")
        print(
            "Type 'y' followed by enter if you can hear the recording correctly."
        )
        print("")
        print(
            "If you can't hear your recording then try again, but next time speak more loudly."
        )
        print(
            "If your micophone has an 'on' switch, make sure it's turned on.")
        print("Check the sound input settings on your operating system.")
        print("")
        print(
            "To stop playback, and to indicate that this check has failed, just press enter."
        )
        print("To repeat this check, type 'r' and then enter.")
        user_input = input()

        if len(user_input) >= 1 and (user_input[0] == "y"
                                     or user_input[0] == "Y"):
            print("Check passed.")
            return True
        elif len(user_input) >= 1 and (user_input[0] == "r"
                                       or user_input[0] == "R"):
            print("Repeating test.")
        else:
            print("Check failed.")
            return False
Esempio n. 8
0
def on_hold(seconds=5):
    fs = 44100
    myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
    sd.wait()  # Wait until recording is finished
    return write('output.wav', fs, myrecording)
Esempio n. 9
0
screenHight = 500
screen = pygame.display.set_mode([screenWitdh, screenHight])

# Run until the user asks to quit
running = True
while running:

    # Did the user click the window close button?
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Fill the background with black
    screen.fill((0, 0, 0))

    myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
    sd.wait()

    N = len(myrecording)
    X = np.fft.rfft(myrecording)
    mul = 11**5

    # calculating energi for the signal
    h1 = mul * (np.real((1 / 1795) * np.sum((X[0 * 1795:1 * 1795]**2))))
    h2 = mul * (np.real((1 / 1795) * np.sum((X[1 * 1795:2 * 1795]**2))))
    h3 = mul * (np.real((1 / 1795) * np.sum((X[2 * 1795:3 * 1795]**2))))
    h4 = mul * (np.real((1 / 1795) * np.sum((X[3 * 1795:4 * 1795]**2))))
    h5 = mul * (np.real((1 / 1795) * np.sum((X[4 * 1795:5 * 1795]**2))))
    h6 = mul * (np.real((1 / 1795) * np.sum((X[5 * 1795:6 * 1795]**2))))
    h7 = mul * (np.real((1 / 1795) * np.sum((X[6 * 1795:7 * 1795]**2))))
Esempio n. 10
0
import numpy as np
import matplotlib.pyplot as plt
import sounddevice as sd
#Data on recording, etc on: https://python-sounddevice.readthedocs.io/en/0.3.12/usage.html#recording
fs = 48000
T = 1 / fs
duration = 2  # seconds
N = int(duration * fs)
x = sd.rec(N, samplerate=fs, channels=2)
print(str(x.shape))
sd.wait()
#sd.play(x,samplerate=fs)
t = np.arange(0, duration, 1 / fs)  # time vector
plt.plot(t, x)
plt.show()
X0 = np.fft.fftshift(np.fft.fft(x[:, 0]) / N)
X1 = np.fft.fftshift(np.fft.fft(x[:, 1]) / N)
f = np.arange(-1 / (2 * T), 1 / (2 * T), 1 / (N * T))
plt.plot(f, np.abs(X0))
plt.show()
Esempio n. 11
0
def rekorduj():
    global lista
    fs = 44100  # frekvencija u hz
    seconds = 5  # trajanje zvuka
    mojrekording = sd.rec(int(seconds * fs), samplerate=fs,
                          channels=2)  # kanali za snimanje
    sd.wait()  # pauziranje kako bi se snimijo zvuk
    write('output.wav', fs,
          mojrekording)  # pisanje i cuvanje zvuka u wav format

    # winsound.Beep(1000, 100)    # beep winsound modul za slusanje snimljenog zvuka
    # filename = "output.wav" # ekstenzija fajla je wav
    # winsound.PlaySound(filename, winsound.SND_FILENAME) #  plejer

    data, samplerate = sf.read(
        'output.wav')  # modul za konvertovanje wav fajla u \
    sf.write('output.flac', data, samplerate)  # flac fajl
    # nisam bas strucan za audio produkciju ali koliko sam procitao razlika izmedju wav i flaca je u frekvenciji i kanala emitovanja
    # i iz tog razloga speech_recognition modul za pisanje zvucnih fajlova u txt fajlove ne moze da koristi wav, mp3.. itd audio formate ali mu odgovara flac

    r = sr.Recognizer()
    hard = sr.AudioFile('output.flac')  # audio fajl koji citamo
    with hard as source:  # otvaranje i konvertovanje audio fajla u tekst
        audio = r.record(source)
    rec_1 = r.recognize_google(
        audio)  # izgovorenu recenicu stavljamo u variablu
    rec_2 = rec_1.split(
        " ")  # zatim pomocu splita rastavljamo recenicu i stavljamo u listu

    lista_cmds = [
        'open', 'copy', 'run', 'search'
    ]  # lista komandi koje mogu da se izvrsavaju npr open ima svoju listu sta moze da otvara dok do nema nista za sad
    lista_za_search = ['YouTube', 'browser']
    lista_za_run = ["backup"]
    lista_za_open = [
        'YouTube', 'browser', 'Python'
    ]  # lista cmds ima komandu open komanda open ima listu sadrzaja sta moze da otvori
    lista_za_copy = ["file", "directory"]
    lista_simbola = ["/", "\\", "c"]
    #print(rec_2)
    provera = any(
        item in rec_2 for item in lista_cmds
    )  # any funkcija pravi proveru da li rec_2 i lista_cmds ima zajednicko nesto ako ima vraca nam bool true ili false ako nema
    if provera == True:  # ako je tacno ulazimo u sledeci blok
        for i in rec_2:  # proveravamo reci iz liste i liste cmds ako postoje pomocu filtera mozemo da je ispisemo

            filt_object = filter(lambda a: i in a, lista_cmds)
            provera_2 = any(item in rec_2 for item in lista_za_open)

            if provera_2 == True:
                filt_object_2 = (filter(lambda b: i in b, lista_za_open))
                konvert = ''.join(
                    list(filt_object)
                )  #filte_object pretvaramo u listu kako bi mogli da utvrdimo da li su izgovorene reci odgovarajuce ovom bloku
                konvert_2 = ''.join(
                    list(filt_object_2)
                )  # flit_ogject je konvertovan u listu ali sa join pretvaramo u string tj. rec koja je izgovorena a pritom je komanda iz list_cmds

                if 'search' in konvert or konvert_2 == True:  # zato sto 'copy' komanda se nalazi u listi i kada kazemo do youtube otvara se yt ili bilo sta drugo morao sam da regulisem ako neko
                    break
                if 'copy' in konvert or konvert_2 == True:  # zato sto 'copy' komanda se nalazi u listi i kada kazemo do youtube otvara se yt ili bilo sta drugo morao sam da regulisem ako neko
                    break  # kaze do da ne otvara stvari koje se otvaraju sa open za to sam koristio bool ako se 'copy' nadje u reci da brejkujemo do sledece linije onda odatle mozemo da dodamo komande za copy
                if (konvert == 'browser' or konvert_2 == 'browser'):
                    subprocess.Popen(
                        "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
                    )  # subprocess otvara chrome ako filter objekti sadrze navedenu recu if uslovu google
                    #break   # nakon otvaranja taba brejkujemo program
                if (
                        konvert == 'YouTube' or konvert_2 == 'YouTube'
                ):  #uslovi za otvaranje su promenjeni vise nema open iz razloga jel taj uslov vec postoji da nismo rekli open ne bi ni dosli do ovog bloka
                    url = 'http://youtube.com/'  # url stranice koju otvaramo
                    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'  # ovo je lokacija chroma
                    webbrowser.get(chrome_path).open(
                        url, new=1
                    )  # prosledjujemo path i otvaramo url u novom prozoru
                    #break   # nakon izvrsavanja brejkujemo program
                if (konvert == 'Python' or konvert_2 == 'Python'):
                    url = 'http://docs.python.org/'
                    chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
                    webbrowser.get(chrome_path).open(url, new=1)
                    #break
            else:
                print("Ova komanda ne moze da izvrsi taj proces")
    else:
        print("Rec nije komanda")

    provera = any(
        item in rec_2
        for item in lista_cmds)  # kreiramo novu proveru za komandu copy
    if provera == True:
        for i in rec_2:
            filt_object = filter(lambda q: i in q, lista_cmds)
            provera_3 = any(item in rec_2 for item in lista_za_copy)

            if provera_3 == True:  # ako komanda file postoji u listi_za_copy ulazimo u blok
                filt_object_3 = (filter(lambda w: i in w, lista_za_copy))
                konvert = ''.join(list(filt_object))
                konvert_3 = ''.join(list(filt_object_3))

                info = """                      Komande za kopiranje fajlova:
1. Ako navodis drive prvo ukucaj 'c' nakon toga izgovori 'CD', opet ukucaj 'c' i izgovori ime drive npr drive'F'.
2. Za navodjenje simbola ukucaj 's' i za nazad izgovori 'backslash' za napred izgovori 'slash'.
3. Za navodjenje imena foldera ukucaj 'n' i izgovori ime foldera. 
4. BITNO: Prilikom navodjenja imena foldera maksimalan broj razmaka u imenu foldera je 3, 
ako u imenu foldera postoji razmak prvo izgovori prvu rec imena foldera npr 'program'\\'file' sa dva backslasha-
se navodi da je razmak izmedju  
"""

                if konvert == 'file' or konvert_3 == 'file':  # provera da li je file u nekoj od varijabli
                    print(info)
                    names = []
                    izlaz = ''
                    recenice = []
                    while izlaz != 'quit':  # WHILE petlja traje sve dok se u listu za izlaz ne upise quit dok se unosi n nastavlja se snimanje reci
                        izlaz = input(
                            "Prati korake postepeno za pravilno koriscenje ove opcije: "
                        )
                        names.append(izlaz)

                        if izlaz == 'quit':  # izlaz iz petlje
                            break

                        print("3")
                        print("2")
                        print("1")

                        fs = 44100
                        seconds = 3
                        mojrekording = sd.rec(int(seconds * fs),
                                              samplerate=fs,
                                              channels=2)
                        sd.wait()
                        write('output.wav', fs, mojrekording)

                        data, samplerate = sf.read('output.wav')
                        sf.write('output.flac', data, samplerate)

                        r = sr.Recognizer()
                        hard = sr.AudioFile('output.flac')
                        with hard as source:
                            audio = r.record(source)
                        navodi = r.recognize_google(audio)
                        navodi_2 = navodi.split(" ")
                        recenice.append(
                            navodi_2
                        )  # dodajemo u listu reci i karaktere koji su uneseni

                        print(recenice)
                        continue

                    file_path = []
                    ls_nm = [
                    ]  # pomocna lista za imena dira ako ime sadrzi dve reci onda je tuple
                    for n in names:
                        if n == 'n':
                            for r in recenice:
                                if len(
                                        r
                                ) == 2:  # upisivanje reci za ime direktorijuma

                                    f = r[0], r[1]
                                    ls_nm.append(f)

                                if len(r) == 3:
                                    e = r[0], r[1], r[2]
                                    file_path.append(e)
                                if len(r) == 4:
                                    o = r[0], r[1], r[2], r[3]
                                    file_path.append(o)
                                else:
                                    print(
                                        'Previse je dugacko ime foldera max razmaka je "3"'
                                    )

                        if n == 's':  # komanda za specijalne simbole
                            for s in recenice:
                                if 'slash' in s:  # konvertovanje reci u simbol
                                    w = '/'
                                    file_path.append(w)
                                if lista_simbola[
                                        1] in s:  # konvertovanje backslasha u simbol \
                                    w = lista_simbola[1]
                                    file_path.append(w)

                        if n == 'c':  # komanda 'CD' za navodjenje direktorijuma
                            for c in recenice:
                                if 'CD' in c:
                                    for d in recenice[1]:
                                        if 'see' in recenice[1]:
                                            d = 'C'
                                            var = d + ":"  # dodavanje dvotacke za drive npr 'F:'
                                            file_path.append(
                                                var
                                            )  # dodajemo u listu var koji ima C:
                                            if len(
                                                    file_path
                                            ) == 2:  # koristimo pop zato sto zbog for petlje dublira korake
                                                print("new is't pop")
                                                file_path.pop(1)
                                        else:
                                            var = d + ":"
                                            file_path.append(var)
                                            if len(file_path) == 2:
                                                print("new is't pop")
                                                file_path.pop(1)
                    for ls in ls_nm:  # for petlja razdvaja listu
                        spoj = " ".join(ls)  # iz tuple daje dve reci

                    dir_path = ''  # variabla putanje do direktorijuma
                    for path in file_path:
                        qw = ''.join(
                            path
                        )  # spajamo izgovorene reci i simbole u u jedno

                        dir_path += qw  # pomocu operatora dodle kreiramo putanju

                    dir_path += spoj  # na kraju se dodaje ime direktorijuma
                    print(dir_path)
                    files = os.listdir(
                        dir_path)  # proverava da li je postojeci direktorijum

                    for item in files:

                        print(
                            "direktori: ", item
                        )  # ispisujemo sve iz direktorijuma sta se nalazi u njemu

                #lista abecede koja ne moze u ovaj program
                # s-see, h-ash, o-oh, p-b, q-kwel, r-are, s-ass, oce/t-d, u-you, v-we, y-why

                if konvert == "directory" or konvert_3 == 'directory':
                    print("directory")

    provera = any(item in rec_2 for item in lista_cmds)
    if provera == True:
        filt_object = filter(lambda a: i in a, lista_cmds)
        provera_2 = any(item in rec_2 for item in lista_za_run)
        if provera_2 == True:

            print("yes it's true")
            os.system(
                'python "c:\\users\\milos\\desktop\\python Pro\\python\\Voice_Text\\copy_dirs.py"'
            )
    provera = any(item in rec_2 for item in lista_cmds)
    if provera == True:
        filt_object = filter(lambda a: i in a, lista_cmds)
        provera_2 = any(item in rec_2 for item in lista_za_search)
        if provera_2 == True:
            filt_object_2 = (filter(lambda b: i in b, lista_za_search))
            konvert = ''.join(list(filt_object_2))
            while True:
                izlaz = ['exit']

                print("Izgovori sta zelis da pretrazis na %s. " % konvert)

                fs = 44100
                seconds = 5
                mojrekording = sd.rec(int(seconds * fs),
                                      samplerate=fs,
                                      channels=2)
                sd.wait()
                write('output.wav', fs, mojrekording)

                data, samplerate = sf.read('output.wav')
                sf.write('output.flac', data, samplerate)

                r = sr.Recognizer()
                hard = sr.AudioFile('output.flac')
                with hard as source:
                    audio = r.record(source)
                navodi = r.recognize_google(audio)
                navodi_2 = navodi.split(" ")

                provera = any(item in navodi_2 for item in izlaz)
                filt_object = filter(lambda a: i in a, izlaz)
                konvert_2 = ''.join(list(filt_object))
                break
                # if provera == True:
                #     print('Zavrsio si sa pretragom')
                #     break
            rec_za_pretragu = " ".join(navodi_2)
            print(rec_za_pretragu)
            if konvert == 'browser':

                url = ('https://www.google.com/?#q=' + rec_za_pretragu
                       )  # url stranice koju otvaramo
                chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'  # ovo je lokacija chroma
                webbrowser.get(chrome_path).open(url, new=1)
Esempio n. 12
0
def capture():
	img_array,labels=face_trainer(100)
	print(labels)
	time.sleep(10)
	recognizer=cv2.createLBPHFaceRecognizer()
	recognizer.train(img_array,np.array(labels))
	frame_count=0
	duration=DURATION
	sd.default.samplerate=22050
	sd.default.channels=1
	myrecording=sd.rec(duration*22050)
	vc=cv2.VideoCapture(0)
	cascade="/home/aniket/Desktop/code/face_recog/haarcascade_classifier/smile.xml"
	rval,frame=vc.read()
	
#path2='/'.join(cascade.split('/')[1:-1])
#print(path2)
#print(os.listdir('/home/aniket/Desktop/code/face_recog/'))

	multiple_face_counter=0
	while rval:
		print('current frame count',frame_count)
		print(multiple_face_counter)
		faceCascade=cv2.CascadeClassifier(cascade)
		image_read=frame
#cv2.imshow("smile found",image_read)

#turning image to grayscale
		gray=cv2.cvtColor(image_read,cv2.COLOR_BGR2GRAY)
		faces=faceCascade.detectMultiScale(
		gray,
		scaleFactor=1.2,
		minNeighbors=5,
		minSize=(30,30),
		flags=cv2.cv.CV_HAAR_SCALE_IMAGE,
		)
	
	
		print('found '+str(len(faces))+
		' in this image \n plotting image')

		if len(faces)==1:
			for(x,y,w,h) in faces:
				if predicter(recognizer,gray[y:(y+h),x:(x+w)])==1:
					cv2.rectangle(image_read, (x,y),(x+w,y+h),(255,0,255),2)
				else:	
					cv2.rectangle(image_read, (x,y),(x+w,y+h),(255,0,0),2)
					multiple_face_counter=multiple_face_counter+1
			cv2.imshow('12104684',image_read)
			cv2.waitKey(40)
		elif len(faces)==0:
			cv2.imshow('12104684',image_read)
			cv2.waitKey(40)
			print('0 face detected')
		else:
			print('more than one face detected')
			for(x,y,w,h) in faces:
				if predicter(recognizer,gray[y:(y+h),x:(x+w)])==1:
					cv2.rectangle(image_read, (x,y),(x+w,y+h),(255,0,255),2)
				else:	
					cv2.rectangle(image_read, (x,y),(x+w,y+h),(255,0,0),2)
					multiple_face_counter=multiple_face_counter+1
			#multiple_face_counter=multiple_face_counter+1
			cv2.imshow("12104684",image_read)
			cv2.waitKey(40)
			try:
				if multiple_face_counter>20:
					a=1/0
			except:
				print('more than one face detected')
		rval,frame=vc.read()
		frame_count=frame_count+1
		if frame_count>MAX_FRAME_RATE:
			break
	return myrecording
Esempio n. 13
0
 def record(self):
     my_recording = sd.rec(
             self.duration * self.fs,
             blocking=True,
             dtype='float32')
     return my_recording
Esempio n. 14
0
def recordSound():
    print("Recording for", duration ,"sec...")
    myrecording = sd.rec(int(duration * fs)) 
    sd.wait()
    myrecording = myrecording[:,0]
    return myrecording
Esempio n. 15
0
            precision_range = ((1-precision)*exact_target, (1+precision)*exact_target)
            if is_in_range(f, precision_range):
                return pitch, 0
            elif f <= exact_target:
                return pitch, 1
            else:
                return pitch, -1

# Define some settings
device = 0    # we use my USB sound card device
duration = 0.5  # seconds
fs = 44100    # samples by second
precision=0.02 # how close to the target pitch do we consider a match

while True:
    print("---------")
    # Listen to a bit of sound from the mic, recording it as a numpy array
    myrecording = sd.rec(duration * fs, samplerate=fs, channels=1, device=device)
    time.sleep(1.1*duration)
    # Calculate the Fourier transform of the recorded signal
    fourier = np.fft.fft(myrecording.ravel())
    # Extrat the fundamental frequency from it 
    f_max_index = np.argmax(abs(fourier[:fourier.size/2]))
    # Get the scale of frequencies corresponding to the numpy Fourier transforms definition
    freqs = np.fft.fftfreq(len(fourier))
    # And so the actual fundamental frequency detected is
    f_detected = freqs[f_max_index]*fs
    print(f_detected)
    # Give relevant guidance to the user (tune up or down)
    display_tuning_guidance(*tuning_guidance(f_detected, precision), duration=0.45)
Esempio n. 16
0
import soundfile as sf
from colorama import init, Fore, Back, Style

# Sampling frequency
freq = 44100

# Recording duration
duration = 3
init(autoreset=True)

input("\nPRESS ENTER TO RECORD YOUR VOICE...")
print(Style.BRIGHT+ Fore.GREEN + "\nSpeak Now...")

# Start recorder with the given values
# of duration and sample frequency
recording = sd.rec(int(duration * freq),
        samplerate=freq, channels=1)

# Record audio for the given number of seconds
sd.wait()
#taking input of speakers name
print(Style.BRIGHT + Fore.GREEN +"\n\nEnter speaker's name : ")
input_label = input()
speaker_name=input_label+".wav"
wv.write(speaker_name, recording, freq, sampwidth=2)

data, samplerate = sf.read(speaker_name)
sf.write(speaker_name, data, samplerate, subtype='PCM_16')

print(Style.BRIGHT + Fore.GREEN +"\nDone.......")

Esempio n. 17
0
import io
import math
from pprint import pprint
from urllib.request import urlopen
import soundfile as sf

url = "http://tinyurl.com/shepard-risset"
data, fs = sf.read(io.BytesIO(urlopen(url).read()))

import sounddevice as sd
sd.play(data, fs)

fs, duration = 44100, 1.5
data = sd.rec(duration * fs, samplerate=fs, channels=2)

sf.write('file.mp3', data, fs)

fs = 44100/20
data = []
msg = 'Hello World!'
time_to_play = 0.5
time_of_char = int(fs*time_to_play/len(msg))

for char in msg:
    asci = ord(char)
    data.extend([[math.cos(_*asci), math.cos(_*asci)] for _ in range(time_of_char)])

sd.play(data, fs, blocking=False)
Esempio n. 18
0
import speech_recognition as sr
import sounddevice as sd
from scipy.io.wavfile import write
import os

fs = 44100  # this is the frequency sampling; also: 4999, 64000
seconds = 5  # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=2)
print("Starting: Speak now!")
sd.wait()  # Wait until recording is finished
print("finished")
write('output.wav', fs, myrecording)  # Save as WAV file

rec = sr.Recognizer()
hearing = sr.AudioFile('output.wav')
with hearing as source:
    audio = rec.record(source)
    text = rec.recognize_google(audio)
    print("Text: ".format(text))
def record_sound(sec, message):
    sr = 16000
    print(f"{message} for {sec} seconds..")
    sound = sd.rec(int(sec * sr), samplerate=sr, channels=1)
    sd.wait()
    return sound, sr
Esempio n. 20
0
import os
import wavio
import pygame
import time
import wave
import numpy
fs= 44100
krow=[0]
duration=.1
a=0
pygame.mixer.init()
while True:
    try:
        hire=[]
        if a%1==0:
            recording = sd.rec(math.ceil(duration * fs), samplerate=fs, channels=2)
            sd.wait()
            fay=[]
            yaf=[]
            fred=math.ceil(fs*duration)-10
            for p in range(1,fred):
                fay.append(recording[p][0])
            for elem in fay:
                yaf.append(abs(elem))
        a=a+1
        
        krow.append(np.mean(yaf))
        for elem in krow:
            hire.append(abs(np.mean(krow)-elem))
        
        if np.mean(krow)<np.mean(yaf)-np.mean(hire):
import sounddevice as sound_device
from scipy.io.wavfile import write as audio_write

fs=44100
seconds=15

recording=sound_device.rec(int(seconds*fs),samplerate=fs,channels=2)
sound_device.wait()
audio_write("recording.wav",fs,recording)
Esempio n. 22
0
def main():
    # Criando o objeto da biblioteca fornecida.
    bib = signalMeu()

    # Tempo de duração da gravação.
    duration = 3

    # Taxa de amostragem.
    sampleRate = 44100

    # Número de canais a serem gravados.
    channels = 1

    time.sleep(1)
    print("3...")

    time.sleep(1)
    print("2...")

    time.sleep(1)
    print("1...")
    print("Recording...")

    # Gravando o áudio.
    audio = sd.rec(int(duration * sampleRate), channels=channels)
    sd.wait()

    #
    audio2 = [a[0] for a in audio]

    # Calculando FFT.
    xf, yf = bib.calcFFT(audio2, sampleRate)

    # Plotando o gráfico do FFT.
    plt.title("Fourier Audio")
    plt.figure("F(y)")
    plt.plot(xf, yf)
    plt.grid()
    plt.show()

    # Pegando o índice de cada pico (onde está na lista).
    indexes = peakutils.indexes(yf, 0.05, min_dist=100)

    # Pegando os picos de acorco com os índices encontrados.
    # Ignoramos os índices que não estão entre 600Hz e 2000Hz.
    peaks = [xf[i] for i in indexes if xf[i] > 600 and xf[i] < 2000]

    # Printando os picos.
    print(f"Peaks: {peaks}")

    # Verificando se os picos estão dentro da tabela de números, incluindo a margem de erro.
    margin = 10
    for entry in dfmtTable:
        number = entry[0]

        first = False
        second = False

        for peak in peaks:
            if (entry[1] - margin <= peak <= entry[1] + margin):
                first = True

            if (entry[2] - margin <= peak <= entry[2] + margin):
                second = True

        if (first and second):
            print(f"Number is: {number}")
Esempio n. 23
0
def record(sample_rate,seconds,output_filename,channels):
    fs = sample_rate  # Sample rate
    myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=channels)
    sd.wait()  # Wait until recording is finished
    write('myfile.wav', fs, myrecording)  # Save as WAV file
Esempio n. 24
0
import io
import math
from pprint import pprint
from urllib.request import urlopen
import soundfile as sf

url = "http://tinyurl.com/shepard-risset"
data, fs = sf.read(io.BytesIO(urlopen(url).read()))

import sounddevice as sd
sd.play(data, fs)

fs, duration = 44100, 1.5
data = sd.rec(duration * fs, samplerate=fs, channels=2)

sf.write('file.mp3', data, fs)

fs = 44100 / 20
data = []
msg = 'Hello World!'
time_to_play = 0.5
time_of_char = int(fs * time_to_play / len(msg))

for char in msg:
    asci = ord(char)
    data.extend([[math.cos(_ * asci), math.cos(_ * asci)]
                 for _ in range(time_of_char)])

sd.play(data, fs, blocking=False)
Esempio n. 25
0
import sounddevice as sd
import numpy as np
import wave

wave_length = 5

sample_rate = 16000

print('録音開始')

data = sd.rec(int(wave_length * sample_rate), sample_rate, channels=1)

sd.wait()

data_scale_adjust = data * np.iinfo(np.int16).max

wave_out = wave.open('./wgn_wave.wav', 'w')

wave_out.setnchannels(1)

wave_out.setsampwidth(2)

wave_out.setframerate(sample_rate)

# wave_out.writeframes(data_scale_adjust)
wave_out.writeframes(data)

wave_out.close()
Esempio n. 26
0
    loud = 0
    beat = 0

    while True:
        myrecording = sd.rec(int(duration), samplerate=fs, channels=1)
        sd.wait()
        loud = np.sum(myrecording**2)
        if loud > 0.1:
            if not q.empty():
                beat = q.get()
            offset = beat - time.time()
            print offset


if __name__ == '__main__':
    print "opening input stream once here"
    testrec = sd.rec(1024, samplerate=44100, channels=1)
    sd.wait()
    print "Input a bpm as an integer"
    bpm = int(raw_input())
    myq = Queue()
    clickproc = Process(target=clicktrack, args=(
        myq,
        bpm,
    ))
    offsetproc = Process(target=offset, args=(myq, ))
    clickproc.start()
    offsetproc.start()
    clickproc.join()
    offsetproc.join()
 def record_user(self, duration):
     duration = duration
     fs = 22050
     user_rec = sd.rec(int(duration * fs), samplerate=fs, channels=1)
     sd.wait()
     return (user_rec)
Esempio n. 28
0
 def _record(self):
     return sounddevice.rec(int(self.duration * self.sample_rate),
                            samplerate=self.sample_rate,
                            blocking=True,
                            channels=1,
                            dtype='float64')
Esempio n. 29
0
                    help="Filename to store array")
parser.add_argument('-t',
                    action='store',
                    type=float,
                    default=1.0,
                    dest='duration',
                    help="Time (in seconds) to record")
parser.add_argument('-f',
                    action='store',
                    type=int,
                    default=16000,
                    dest='sample_rate',
                    help="Sample rate (Hz)")
args = parser.parse_args()
print("Setting up recording with:")
print(args.sample_rate, "Hz")
print(args.duration, "s")
print(args.output_file)
time.sleep(1.0)

# Do recording
print("Recording...")
rec = sd.rec(int(args.duration * args.sample_rate),
             samplerate=args.sample_rate,
             channels=num_channels)
sd.wait()
print("Done!")

# Save array
print("Saving to", args.output_file)
np.save(args.output_file, rec)
Esempio n. 30
0
def receive(time=150 * lib.TIME_BY_CHUNK + lib.NOISE_TIME):
    sd.default.channels = 1
    record = sd.rec(int(np.ceil(time * lib.FS)), lib.FS, blocking=True)
    return record[:, 0]
Esempio n. 31
0
    # Extract mono channels from input data.
    ch1 = np.array(audiodata[::downsample, 0], dtype=np.float32)
    ch2 = np.array(audiodata[::downsample, 1], dtype=np.float32)

    # High-pass filter the data at a cutoff frequency of 10Hz.
    # This is required because I2S microhones have a certain DC offset
    # which we need to filter in order to amplify the volume later.
    ch1 = butter_highpass_filter(ch1, 10, samplerate)
    ch2 = butter_highpass_filter(ch2, 10, samplerate)

    # Amplify audio data.
    # Recommended, because the default input volume is very low.
    # Due to the DC offset this is not recommended without using
    # a high-pass filter in advance.
    ch1 = set_gain_db(ch1, input_gain_db)
    ch2 = set_gain_db(ch2, input_gain_db)

    # Output the data in the same format as it came in.
    return np.array([[ch1[i], ch2[i]] for i in range(len(ch1))], dtype=np.float32)

# Record stereo audio data for the given duration in seconds.
rec = sd.rec(int(seconds * samplerate), samplerate=samplerate, channels=2)
# Wait until the recording is done.
sd.wait()

# Process the audio data as explained above.
processed = process_audio_data(rec)

# Write the processed audio data to a wav file.
write('out.wav', int(samplerate/downsample), processed)
Esempio n. 32
0
 def record(self, seconds):
     self.sound_array = sd.rec(int(seconds * self.fs),
                               samplerate=self.fs,
                               channels=2)
     return self.sound_array
 def record(self, duration, fs):
     myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=2)
     sd.wait()
     timestr = time.strftime("%Y%m%d-%H%M%S") + '.wav'
     sf.write(timestr, myrecording, samplerate=44100)
Esempio n. 34
0
    # Número de canais a serem gravados.
    channels = 1

    time.sleep(1)
    print("3...")

    time.sleep(1)
    print("2...")

    time.sleep(1)
    print("1...")
    print("Gravando...")

    # Gravando o áudio.
    audio = sd.rec(int(duration * fs), channels=channels)
    sd.wait()

    # Pegando o vetor correto.
    audio = [a[0] for a in audio]
    audio = np.array(audio)

    # Calculando FFT.
    xf, yf = bib.calcFFT(audio, fs)

    # Plotando o gráfico do FFT.
    plt.title("Fourier Audio")
    plt.figure("F(y) AM recebido")
    plt.plot(xf, yf)
    plt.grid()
def record():

    myrecording = sd.rec(int(SECONDS*SAMPLE_RATE), samplerate=SAMPLE_RATE, channels=1)
    sd.wait()  # Wait until recording is finished

    return myrecording
Esempio n. 36
0
def sync_record(filename, duration, fs, channels):
    print('recording')
    myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=channels)
    sd.wait()
    sf.write(filename, myrecording, fs)
    print('done recording')
Esempio n. 37
0
# create a test signal with frequency in Hz:
f = 500
# np.sin() function takes an ndarray as argument, where the sin is calculated for every point of the array. n is discrete time:
#x = np.sin(2*pi*f*t) + np.sin(2*pi*2.4*f*t) + np.sin(2*pi*6.75*f*t)

# the problem is that python (or the application) has not requested permission to access the internal microphone on
# the mac. In system preferences -> security and privacy -> Privacy we see the applications that have requested permissions,
# but we cannot add more. A possible solution was proposed: do the following (possibly with a sudo to get superuser access):
# rm -rf ~/Library/Application\ Support/com.apple.TCC
# (https://www.reddit.com/r/MacOS/comments/9lwyz0/mojave_not_privacy_settings_blocking_all_mic/)
# get full disk access to terminal by going to Full disk access and clicking on terminal.
# Problem solved! I need to run it from terminal, so it requests access to the microphone.

print("recording...")
# first parameter is the number of frames (samples) to record, ie. duration * Fs. Stores in numpy array
x = sd.rec(duration * Fs, samplerate=Fs, channels=1)
# wait for recording to finish, otherwise it records noise:
sd.wait()

# playback the recording:
sd.play(x, Fs)
# write it to file as .wav:
#sf.write("./out.wav", x, Fs)
print(np.shape(x))

# plot two plots in two rows and one column, this is the first one:
plt.subplot(2, 1, 1)
plt.xlabel("Time (s)")
plt.ylabel("Magnitude")
plt.plot(t, x)
Esempio n. 38
0
    )
    args = parser.parse_args()

    # Prepare one-shot STFT
    L = args.block
    # Let's hard code sampling frequency to avoid some problems
    fs = 16000

    # RECORD
    if args.device is not None:
        sd.default.device[0] = args.device

    # MIXING
    print("* Recording started... ", end="")
    mics_signals = sd.rec(int(args.duration * fs),
                          samplerate=fs,
                          channels=2,
                          blocking=True)
    print("done")

    # STFT ANALYSIS
    # shape == (n_chan, n_frames, n_freq)
    X = pra.transform.stft.analysis(mics_signals.T,
                                    L,
                                    L,
                                    zp_back=L // 2,
                                    zp_front=L // 2)

    # Monitor convergence
    it = 10

    def cb_print(*args):
Esempio n. 39
0
    def getSignal(self):
        """
        Método que pega o sinal enviado por áudio pelo emissor e trata-o para adquirir a tecla que foi
        enviada
        """
        bib = bibSignal.signalMeu()
        sd.default.samplerate = self.freqAmostra
        sd.default.channels = 2
        print("\nA captura de áudio iniciará em 5 segundos")
        t0 = time.time()
        t1 = time.time()
        while (t1 - t0 < 5):
            print(str(round(5 - (t1 - t0))) + str(" segundo(s)\r"), end='\r')
            t1 = time.time()
            time.sleep(1)
        print("")
        print("\n====================")
        print("Começando a gravar!")
        print("====================\n")

        audio1 = sd.rec(int(self.duration * self.freqAmostra),
                        self.freqAmostra,
                        channels=1)

        sd.wait()
        audio = []
        for sublist in audio1:
            for item in sublist:
                audio.append(item)
        print("")
        print("\n======================")
        print("Finalizando gravação!")
        print("======================\n")
        #print(audio)
        t = np.linspace(0, self.duration, self.duration * self.freqAmostra)
        #print(f"t = {t}")
        #print(f'len(t) = {len(t)}')
        xf, yf = bib.calcFFT(audio, self.freqAmostra)
        # print(f'xf = {xf}')
        # print(f'yf = {yf}')
        indexes = peakutils.indexes(yf, thres=0.2, min_dist=100)
        linha = [697, 770, 852, 941]
        coluna = [1209, 1336, 1477, 1633]
        array = [['1', '2', '3', 'A'], ['4', '5', '6', 'B'],
                 ['7', '8', '9', 'C'], ['X', '0', '#', 'D']]

        pico1 = 0
        pico2 = 0
        for i in linha:
            if (math.isclose(i, xf[indexes[0]], abs_tol=10)):
                pico1 = i
        for j in coluna:
            if (math.isclose(j, xf[indexes[1]], abs_tol=10)):
                pico2 = j

        l1 = linha.index(pico1)
        c1 = coluna.index(pico2)
        tecla = array[l1][c1]
        print('\n---------------')
        print(f'Pico 1 = {pico1} Hz\nPico 2 = {pico2} Hz')
        print('---------------\n')
        print('\n==========================')
        print(f"Foi teclado a entrada: [{tecla}]")
        print('==========================\n')

        bib.plotFFT(audio, self.freqAmostra)

        sys.exit()
Esempio n. 40
0
	return model_from_json(model)

def load_weights(iteration=0, path=test_series_name):
	global model
	if load_from_previous_trial:
		load_path = "output/{}.{}.{}.hdf5".format(path, trial_to_load, iteration)
	else:
		load_path = "output/{}.hdf5".format(path)
	model.load_weights(load_path)

model = load_model(iteration=trial_iteration_to_load)
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
load_weights(iteration=trial_iteration_to_load)

initial_data_feed = sd.rec(sample_duration*44100, samplerate=44100, channels=1, blocking=True)
sd.wait()
data_feed = np.asarray(initial_data_feed)
data_feed = np.reshape(data_feed, (1, sample_duration*44100))

while True:
	# Main loop: this won't ever stop, this is intended to run indefinitely until quit by the user.
	result = model.predict(data_feed, batch_size=1, verbose=0)
	print(result[0])
	## Send OSC Message
	msg = osc_message_builder.OscMessageBuilder(address = "/tuio2/tok")
	msg.add_arg(0, arg_type="i")
	msg.add_arg(10003, arg_type="i")
	msg.add_arg(result[0][0], arg_type="f") #first value
	msg.add_arg(result[0][1], arg_type="f") #second value
	msg.add_arg(result[0][2], arg_type="f") #third value
Esempio n. 41
0
duration = 5  # seconds
filename = 'output.wav'
#need to be mono channel (1)

while True:

    print(
        "You can now minimize the terminal script window and use F11 to make the program start listening or \nPress CTRL+C to exit the program \nHint: To say 'google-chrome-stable' say google dash chrome dash stable \nThis currently does not support commands with multiple words unless they're separated by a dash, slash or dot"
    )

    print("Press F11 to start listening")
    keyboard.add_hotkey('F11', print, args=['F11 was pressed!'])
    keyboard.wait('F11')

    recording = sd.rec(int(samplerate * duration),
                       samplerate,
                       channels=1,
                       blocking=True)
    sf.write(filename, recording, samplerate)

    #initializes new client
    client = speech.SpeechClient()

    #gets the filepath of the filename
    file_name = os.path.dirname(filename)

    # Loads the audio into memory
    with io.open(filename, 'rb') as audio_file:
        content = recording.read()
    audio = types.RecognitionAudio(content=content)

    config = types.RecognitionConfig(
Esempio n. 42
0
#!/usr/bin/env python

# MagPy - Magstripe reader

import sounddevice as sd

fs = 44100
duration = 5  # seconds

sd.default.samplerate = fs
sd.default.channels = 2
#sd.default.device = 0

print('RECORDING')
rec = sd.rec(duration*fs,device=1)

sd.wait()

#for r in rec:
#    print r

print('PLAYING')
sd.play(rec, fs, device=2)

sd.wait()
Esempio n. 43
0
def record_data(filename, duration, fs, channels):
    # synchronous recording 
    myrecording = sd.rec(int(duration * fs), samplerate=fs, channels=channels)
    sd.wait()
    sf.write(filename, myrecording, fs)
Esempio n. 44
0
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 21 19:55:13 2016

@author: SRINIVAS
"""

import sounddevice as sd
import numpy as np
print ("hello")

fs= 44100
duration=4
recording = sd.rec(duration * fs, samplerate=fs, channels=2)
sd.wait()
fay=[]
way=[]
for p in range(1,fs*duration):
    fay.append(recording[p][0])
    way.append(recording[p][0])

me=open('A.txt','a')

for elem in fay:
    me.write(str(elem))
    me.write('\n')
    
me.close()

em=open('B.txt','a')
for elem in way:
Esempio n. 45
0
# sudo pip install sounddevice

import sounddevice as sd
import numpy as np

# fixme: записать в *.wav

#
fs = 1000#48000

sd.default.samplerate = fs
sd.default.dtype = 'int16'  # fixme: uint?
sd.default.channels = 1

duration = 15
X = sd.rec(duration * fs)
X = (X.T)[0]

sd.wait()
# Fs = fs            #% Sampling frequency
# T = 1./Fs             #% Sampling period
# L = 1000             #% Length of signal

# t = colon(0, L-1)*T        #% Time vector
# X = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t)

P1, f = fft_one_side( X , size( X ), fs )
#plot( X )
plot( f, P1 )
grid()
show()