노현욱

feat : things

import pyaudio
import numpy as np
# Define constants
CHUNK_SIZE = 1024 # Number of audio frames per buffer
FORMAT = pyaudio.paInt16 # Audio format (16-bit int)
CHANNELS = 1 # Mono audio
RATE = 44100 # Sample rate (Hz)
RMS_REF = 1.0 # Reference RMS amplitude for dB calculation
# Initialize PyAudio stream
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK_SIZE)
# Main loop
while True:
# Read audio data from stream
data = stream.read(CHUNK_SIZE)
# Convert audio data to numpy array
audio_array = np.frombuffer(data, dtype=np.int16)
# Calculate RMS amplitude
rms = np.sqrt(np.mean(np.square(audio_array)))
# Calculate dB level
avg_db = 20 * np.log10(rms / RMS_REF)
max_db = 20 * np.log10(np.max(audio_array) / RMS_REF)
# Print dB level to console
try:
if(avg_db != np.NaN and max_db != np.NaN and max_db > 50):
print("dB level:", int(avg_db), int(max_db))
except:
continue
\ No newline at end of file
"""PyAudio Example: Audio wire between input and output. Callback version."""
import time
import sys
import pyaudio
import numpy as np
DURATION = 5 # seconds
delay_buffer = np.zeros((44100, 2), dtype=np.float32)
def callback(in_data, frame_count, time_info, status):
global delay_buffer
audio_data = np.frombuffer(in_data, dtype=np.float32).reshape(frame_count, 2)
delayed_data = np.concatenate((delay_buffer, audio_data))
delay_buffer = delayed_data[frame_count:]
return (audio_data + 0.5 * delay_buffer).tobytes(), pyaudio.paContinue
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(2),
channels=1,
rate=44100,
input=True,
output=True,
frames_per_buffer=1024,
stream_callback=callback)
start = time.time()
while stream.is_active() and (time.time() - start) < DURATION:
time.sleep(0.1)
stream.close()
p.terminate()
\ No newline at end of file
import pyaudio
import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt
# 파라미터 설정
RATE = 44100 # 샘플링 주파수
CHUNK = 1024 # 읽을 샘플의 수
THRESHOLD = 256 # 피크를 검출하기 위한 threshold 값
WIN_SIZE = 1024 # STFT를 적용할 윈도우 사이즈
HOP_SIZE = 512 # STFT에서 윈도우 사이의 거리 (오버랩 사이즈)
# PyAudio 객체 생성
p = pyaudio.PyAudio()
# 콜백 함수 정의
def process_audio(in_data, frame_count, time_info, status):
# 오디오 데이터 변환
data = np.frombuffer(in_data, dtype=np.int16)
# STFT 수행
f, t, Zxx = signal.stft(data, RATE, nperseg=WIN_SIZE, noverlap=HOP_SIZE)
# 피크 검출
peaks, _ = signal.find_peaks(np.abs(np.mean(Zxx, axis=1)), height=THRESHOLD, distance=WIN_SIZE)
# 파라미터 추정
if len(peaks) > 0:
peak_idx = peaks[0] # 첫 번째 피크 선택
height = np.abs(Zxx[peak_idx, 0]) # 피크의 높이 추정
freq = f[peak_idx] # 피크의 주파수 추정
amp = np.max(np.abs(data)) # 신호의 진폭 추정
progress = (peak_idx + HOP_SIZE) / RATE # 충돌음의 진행 길이 추정
# 결과 출력
print("Height: {:.2f}, Frequency: {:.2f}, Amplitude: {:.2f}, Progress: {:.2f}".format(height, freq, amp, progress))
# 반환할 데이터 없음
return (in_data, pyaudio.paContinue)
# 입력 스트림 열기
stream = p.open(format=p.get_format_from_width(2),
channels=1,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK,
stream_callback=process_audio
)
# 스트림 시작
stream.start_stream()
# 프로그램 실행 중지 전까지 무한 대기
while stream.is_active():
pass
# 스트림과 PyAudio 객체 종료
stream.stop_stream()
stream.close()
p.terminate()
import sys
import numpy as np
import pyaudio
RECORD_SECONDS = 5
CHUNK = 128
RATE = 44100
DELAY = 0.1 # Delay time in seconds
GAIN = 1 # Echo gain (0 to 1)
# Create buffer for delayed audio data
buffer_size = int(RATE * DELAY)
buffer = np.zeros(buffer_size, dtype=np.int16)
def add_echo(in_data, frame_count, time_info, status_flags):
global buffer
data = np.frombuffer(in_data, dtype=np.int16)
output = data + GAIN * buffer[:len(data)]
buffer = np.roll(buffer, len(data))
buffer[-len(data):] = data
return (output.astype(np.int16).tobytes(), pyaudio.paContinue)
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(2),
channels=1 if sys.platform == 'darwin' else 2,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK,
stream_callback=add_echo
)
print('* recording')
stream.start_stream()
while stream.is_active():
# Do other processing here if necessary
pass
stream.stop_stream()
stream.close()
p.terminate()
No preview for this file type
import base64
task_list = []
def display_menu():
print("일정 관리자")
print("1. 일정 추가")
print("2. 일정 보기")
print("3. 일정 완료 표시")
print("4. 종료")
def add_task():
title = input("일정 제목 입력: ")
description = input("일정 설명 입력: ")
status = "하는 중"
task = { "title": title, "description": description, "status": status}
task_list.append(task)
print("일정이 추가되었습니다.")
def view_tasks():
if not task_list:
print("일정 목록이 비어 있습니다.")
else:
print()
print("일정 목록:")
print("----------------")
for task in task_list:
print(f"제목: {task['title']}")
print(f"설명: {task['description']}")
print(f"상태: {task['status']}")
print("----------------")
def mark_task_complete():
if not task_list:
print("일정 목록이 비어 있습니다.")
return
title = input("완료로 표시할 일정의 제목 입력: ")
for task in task_list:
if task['title'] == title:
task['status'] = "완료"
print("일정이 완료로 표시되었습니다.")
return
print("식별자와 일치하는 일정을 찾을 수 없습니다.")
while True:
display_menu()
choice = input("선택: ")
if choice == "1":
add_task()
elif choice == "2":
view_tasks()
elif choice == "3":
mark_task_complete()
elif choice == "4":
print("프로그램을 종료합니다.")
break
else:
print("올바른 선택지를 입력하세요.")
print()
import pyaudio
from pydub import AudioSegment
from pydub.effects import normalize
# set up PyAudio
pa = pyaudio.PyAudio()
stream = pa.open(format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=1024)
# record some audio from the microphone
audio_data = []
for i in range(0, int(44100 / 1024 * 5)):
data = stream.read(1024)
audio_data.append(data)
# convert the audio data to a PyDub audio segment
audio_segment = AudioSegment(
data=b''.join(audio_data),
sample_width=2,
frame_rate=44100,
channels=1
)
# apply an echo effect to the audio segment
echoed_segment = normalize(audio_segment)
# save the output audio file
echoed_segment.export("output.mp3", format="mp3")
# clean up
stream.stop_stream()
stream.close()
pa.terminate()
import sys
import numpy as np
import pyaudio
import librosa
RECORD_SECONDS = 5
CHUNK = 1024
RATE = 44100
DELAY = 0.1 # Delay time in seconds
GAIN = 1 # Echo gain (0 to 1)
MAX_FREQ = 3000
# Create buffer for delayed audio data
buffer_size = int(RATE * DELAY)
buffer = np.zeros(buffer_size, dtype=np.int16)
def add_echo(in_data, frame_count, time_info, status_flags):
global buffer
data = np.frombuffer(in_data, dtype=np.int16)
def get_max_average_db(data):
data_float = data.astype(np.float32)
# Compute the power spectrogram of the data
S = librosa.stft(data_float, n_fft=2048, hop_length=512)
S_power = np.abs(S)**2
# Convert power spectrogram to dB scale
S_dB = librosa.amplitude_to_db(S_power, ref=np.max)
# Calculate the average dB level
avg_dB = np.mean(S_dB)
max_dB = np.max(S_dB)
return avg_dB, max_dB
def get_dominant_freq(data):
data = data.astype(np.float32) / 32768.0
# Compute the Fourier transform of the data
fft_data = np.fft.fft(data)
# Compute the power spectral density of the data
psd_data = np.abs(fft_data)**2
# Define the frequency range of interest
freqs = np.fft.fftfreq(len(psd_data), d=1/RATE)
# Compute the power spectrogram on the mel scale
S = librosa.feature.melspectrogram(y=data, sr=RATE, n_fft=2048, hop_length=1024)
# Find the frequency bin with the maximum energy in each frame
max_bin = np.argmax(S, axis=0)
# Find the dominant frequency in each frame
dominant_freqs = freqs[max_bin]
# Compute the median of the dominant frequencies to get the overall dominant frequency
dominant_freq = np.median(dominant_freqs)
return dominant_freq
freq = get_dominant_freq(data)
avg_db, max_db = get_max_average_db(data)
print(int(freq), int(avg_db), int(max_db))
temp_gain = freq/MAX_FREQ
output = data + freq/2500 * buffer[:len(data)]
buffer = np.roll(buffer, len(data))
buffer[-len(data):] = data
return (output.astype(np.int16).tostring(), pyaudio.paContinue)
p = pyaudio.PyAudio()
stream = p.open(format=p.get_format_from_width(2),
channels=1 if sys.platform == 'darwin' else 2,
rate=RATE,
input=True,
output=True,
frames_per_buffer=CHUNK,
stream_callback=add_echo
)
print('* recording')
stream.start_stream()
while stream.is_active():
# Do other processing here if necessary
pass
stream.stop_stream()
stream.close()
p.terminate()
import numpy as np
import pyaudio
import time
pa = pyaudio.PyAudio()
delay_buffer = np.zeros((44100, 1), dtype=np.float32)
def callback(in_data, frame_count, time_info, status):
global delay_buffer
audio_data = np.frombuffer(in_data, dtype=np.float32).reshape(1024, 1)
delayed_data = np.concatenate((delay_buffer, audio_data))
delay_buffer = delayed_data[frame_count:]
return (audio_data + 0.5 * delay_buffer).tobytes(), pyaudio.paContinue
stream = pa.open(format=pyaudio.paFloat32,
channels=1,
rate=1024,
input=True,
output=True,
frames_per_buffer=44100,
stream_callback=callback)
start = time.time()
DURATION = 30
# keep the stream running for a few seconds
while stream.is_active() and (time.time() - start) < DURATION:
time.sleep(0.1)
stream.stop()
stream.close()
pa.terminate()
\ No newline at end of file
import pyaudio
import numpy as np
import matplotlib.pyplot as plt
import librosa
import threading
import sys
print = sys.stdout.write
# Define constants for audio parameters
FORMAT = pyaudio.paFloat32
CHANNELS = 1
RATE = 44100
FRAMES_PER_BUFFER = 1024
DELAY = 0.1
GAIN = 0.5
# Open an audio stream
stream = pyaudio.PyAudio().open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=FRAMES_PER_BUFFER)
sound = []
def get_stream_data():
global sound
sound.append(stream.read(FRAMES_PER_BUFFER*5, False))
get_stream_data()
# Create buffer for delayed audio data
buffer_size = int(RATE * DELAY)
buffer = np.zeros(buffer_size, dtype=np.int16)
def add_echo(in_data, frame_count, time_info, status_flags):
global buffer
data = np.frombuffer(in_data, dtype=np.int16)
output = data + GAIN * buffer[:len(data)]
buffer = np.roll(buffer, len(data))
buffer[-len(data):] = data
return (output.astype(np.int16).tostring(), pyaudio.paContinue)
def get_max_average_db():
global sound
data = sound[-1]
# Convert data to numpy array
data_float = np.frombuffer(data, dtype=np.float32)
# Compute the power spectrogram of the data
S = librosa.stft(data_float, n_fft=2048, hop_length=512)
S_power = np.abs(S)**2
# Convert power spectrogram to dB scale
S_dB = librosa.amplitude_to_db(S_power, ref=np.max)
# Calculate the average dB level
avg_dB = np.mean(S_dB)
print("Average dB: {:.2f}".format(avg_dB) + " "+ "Max dB: {:.2f}".format(np.max(S_dB)) + "\n")
def print_dominant_freq():
global sound
data = sound[-1]
# Convert data to numpy array
data = np.frombuffer(data, dtype=np.float32)
# Compute the Fourier transform of the data
fft_data = np.fft.fft(data)
# Compute the power spectral density of the data
psd_data = np.abs(fft_data)**2
# Define the frequency range of interest
freqs = np.fft.fftfreq(len(psd_data), d=1/RATE)
# Compute the power spectrogram on the mel scale
S = librosa.feature.melspectrogram(y=data, sr=RATE, n_fft=2048, hop_length=1024, n_mels=512)
# Find the frequency bin with the maximum energy in each frame
max_bin = np.argmax(S, axis=0)
# Find the dominant frequency in each frame
dominant_freqs = freqs[max_bin]
# Compute the median of the dominant frequencies to get the overall dominant frequency
dominant_freq = np.median(dominant_freqs)
print("Dominant frequency: {:.2f} Hz\n".format(dominant_freq))
threading.Thread(target=get_stream_data).start()
while True:
get_data = threading.Thread(target=get_stream_data)
calc_data = threading.Thread(target=print_dominant_freq)
#get_decibel = threading.Thread(target=get_max_average_db)
get_data.start()
calc_data.start()
#get_decibel.start()
get_data.join()
sys.stdin.flush()
시나리오 작성 먼저 해보기. -> 때리는 소리따라 나오게,
시나리오에 따라서 딜레이를 줄이는 방식을 좀 더 만들어 보자.
소리 왜곡방법은 알아서 재밌어 보이는걸로 해보자.
지향성마이크도 한번 달아보기.
1. 실제 소리변형으로 뭔가 만들기
- 실제 소리가 들릴거냐 말거냐
2. identification만 하고, 다른 소리 재생도 할 수 있음.
\ No newline at end of file
import sys
import numpy as np
import pyaudio
import matplotlib.pyplot as plt
RECORD_SECONDS = 5
CHUNK = 1024
......@@ -16,15 +17,54 @@ stream = p.open(format=p.get_format_from_width(2),
print('* recording')
# Initialize plot
fig, ax = plt.subplots()
x = np.arange(0, RECORD_SECONDS, CHUNK / RATE)
line, = ax.plot(x, np.zeros(len(x)))
def add_echo(data, output_stream):
output_stream.write(data)
# Initialize data arrays
db_data = np.zeros(len(x))
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
add_echo(stream.read(CHUNK), stream)
byte_stream = stream.read(CHUNK)
data = np.frombuffer(byte_stream, dtype=np.int16)
fft_data = np.fft.fft(data)
# 주파수 대역 설정
freq_range = (200, 2000) # 200Hz ~ 2kHz
# 주파수 스펙트럼에서 주파수 대역 추출
freq_spectrum = fft_data[(freq_range[0] // (RATE // len(data))) : (freq_range[1] // (RATE // len(data)))]
# 에너지 계산
energy = np.sum(np.abs(freq_spectrum)**2)
# 소리의 세기 계산
db = 10 * np.log10(energy)
# 소리의 높낮이 계산
max_freq = (np.argmax(np.abs(freq_spectrum)) * RATE) / len(data)
print("freq : ", max_freq)
print("db : ", db)
# Add data to arrays
db_data = np.roll(db_data, -1)
db_data[-1] = db
# Update plot
line.set_ydata(db_data)
ax.relim()
ax.autoscale_view()
plt.draw()
plt.pause(0.001)
print('* done')
stream.close()
p.terminate()
\ No newline at end of file
p.terminate()
# Show plot
plt.show()
......