From 18002eec9c7a479a1df09389f09ca0e0f4780885 Mon Sep 17 00:00:00 2001 From: AtlasC0R3 <39468657+AtlasC0R3@users.noreply.github.com> Date: Sun, 7 Feb 2021 13:28:35 -0500 Subject: [PATCH] Initial commit --- LICENSE | 14 +++++++++ README.md | 3 ++ main.py | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ ntReqs.txt | 3 ++ posixReqs.txt | 3 ++ 5 files changed, 106 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 main.py create mode 100644 ntReqs.txt create mode 100644 posixReqs.txt diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5a8e332 --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..25b2900 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +This is a really dumb program that detects whenever you talk or make noise and makes the volume go up to 100% + +*my ears are totally fine* diff --git a/main.py b/main.py new file mode 100644 index 0000000..bdd1646 --- /dev/null +++ b/main.py @@ -0,0 +1,83 @@ +""" + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + Version 2, December 2004 + + Copyright (C) 2004 Sam Hocevar + + Everyone is permitted to copy and distribute verbatim or modified + copies of this license document, and changing it is allowed as long + as the name is changed. + + DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. You just DO WHAT THE FUCK YOU WANT TO. +""" + +import os +import numpy as np +import sounddevice as sd +from subprocess import call, DEVNULL +import time +if os.name == 'posix': + import alsaaudio +elif os.name == 'nt': + from ctypes import cast, POINTER + from comtypes import CLSCTX_ALL + from pycaw.pycaw import AudioUtilities, IAudioEndpointVolume + import math + # See why I hate Windows? + + +def get_avg(): + duration = 3 # seconds + + avgvolumes = [] + def print_sound(indata, outdata, frames, time, status): + volume_norm = np.linalg.norm(indata)*10 + avgvolumes.append(int(volume_norm)) + # print(int(volume_norm)) + + with sd.Stream(callback=print_sound): + sd.sleep(duration * 1000) + + totalvol = 0 + for vol in avgvolumes: + totalvol = totalvol + vol + avgvol = totalvol / len(avgvolumes) + avgvol = round(avgvol, ndigits=2) + return avgvol + +print('Calibrating... Please shut up and don\'t talk.') +avgvol = get_avg() +print(f'Average silent volume is {avgvol}') + +if os.name == 'posix': + m = alsaaudio.Mixer() +elif os.name == 'nt': + devices = AudioUtilities.GetSpeakers() + interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None) + volume = cast(interface, POINTER(IAudioEndpointVolume)) + +while True: + thing = get_avg() + if thing - avgvol > 1.5: + print("you spoke, volume go up") + bruh = time.time() + 5 + if os.name == 'posix': + oldvol = int(m.getvolume()[0]) + elif os.name == 'nt': + oldvol = volume.GetMasterVolumeLevel() + while True: + if os.name == 'posix': + call(["amixer", "-D", "pulse", "sset", "Master", "100%", "on"], stdout=DEVNULL) + elif os.name == 'nt': + volume.SetMasterVolumeLevel(0, None) + if time.time() > bruh: + break + if os.name == 'posix': + call(["amixer", "-D", "pulse", "sset", "Master", f"{oldvol}%", "on"], stdout=DEVNULL) + elif os.name == 'nt': + volume.SetMasterVolumeLevel(oldvol, None) + else: + print("you passed the vibe check") diff --git a/ntReqs.txt b/ntReqs.txt new file mode 100644 index 0000000..280a76f --- /dev/null +++ b/ntReqs.txt @@ -0,0 +1,3 @@ +numpy +sounddevice +pycaw \ No newline at end of file diff --git a/posixReqs.txt b/posixReqs.txt new file mode 100644 index 0000000..8e592b2 --- /dev/null +++ b/posixReqs.txt @@ -0,0 +1,3 @@ +numpy +sounddevice +pyalsaaudio \ No newline at end of file