Copyright (c) 2021 lorry_rui , Newark ,USA
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*for manufacture test sweep tone sound file ,which also do combined some package from opensource. audio file analyze USE only @ Lorry RUi
- Can set speaker and microphone level and get current friend_name
- Can Open write sound file. like WAV/MP3 file read/write ( SOUNDFILE)
- Can play(can set master speaker volume) and recording through microphone, which can point the
- specific Mic. ( Sounddevice)
- Can get single tone power and THD+N
- can get sweep tone FR , THD, rub/buzz
- can measure sound Spectrogram (For ML or noise study) ( librosa)
Mail to: :lorryruizhihua@gmail.com
:lrui@logitech.com
https://pypi.org/project/LvAut
https://github.com/Lorrytoolcenter/LvAut
|tests| |coverage| |docs| |python-versions| |license|
import LvAut.lvaut_THD as AUT
filename='yourfile.wav'
signal, sample_rate, channels=AUT.load(filename)
from LvAut.sound_level import setspeakervolume
setspeakervolume(40) ## set master speaker volume from 0 to 100
import LvAut.lvaut_THD as AUT
a=AUT.speaker_volume(50)## set current speaker volume from 0 to 100(if mute will unmute),return reading after set
#a=AUT.speaker_volume(50,False) ## set current speaker volume from 0 to 100(never touch mute),return reading after set
#a=AUT.speaker_volume(50,False,False) ## only readout volume
### set Microphone
b=AUT.mic_level(50)## set current microphone level from 0to 100(if mute will unmute),return reading after set
#b=AUT.mic_level(50,False)## set current microphone level from 0to 100(never touch mute),return reading after set
#b=AUT.mic_level(50,False,False)## only readout volume
print(a,b)
import LvAut.lvaut_THD as AUT
a=AUT.speaker_all(50)## set all speakers volume from 0 to 100,return how many speaker done set
b=AUT.mic_all(50)## set all microphones level from 0to 100,return how many mics done set
print(a,b)
import LvAut.lvaut_THD as AUT
a=AUT.get_currentSpeakname() ## get current speaker name
b=AUT.get_currentMicname() ## get current Microphone name
print(a,b)
import LvAut.lvaut_THD as AUT
AUT.playsoundWin('yourfile.wav')
import LvAut.device as sd
print(sd.query_devices())
import LvAut.device as sd
import LvAut.lvaut_THD as AUT
fs = 44100 # Sample rate
seconds = 5 # Duration of recording
myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1) # using default mic
#myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1, device="Microphone (Logitech Webcam C930e), Windows DirectSound")
sd.wait() # Wait until recording is finished
AUT.write("test.wav",myrecording,fs)
import LvAut.lvaut_THD as AUT
import LvAut.soundload as sf
import LvAut.device as sd
data, fs = sf.read(your sound file, frames=-1, start=None, stop=None, dtype=None, always_2d=False,
fill_value=None, out=None, samplerate=None, channels=None,
format=None, subtype=None, endian=None, closefd=True)
myrecording = sd.playrec(data, samplerate=fs, channels=1, dtype=None,
out=None, input_mapping=None, output_mapping=None, blocking=False,
device=device) # if None , will use default device
sd.wait() # Wait until recording is finished
AUT.write(outfile,myrecording,fs,'PCM_32') ## 'PCM_16','PCM_32', 'FLOAT', 'DOUBLE'
import LvAut.lvaut_THD as AUT
filename='Device_Mic_THD_R_3.wav'
trigeFrequncy=400 ## this need sweep from high(above 400) to low sweep tone
stopananlysis=100 ## stop analyze_sweep
channaelselect=1 ### if recording is dual channel ,leftchannel=1, rightchannel=2, otherwise no need to define
freq,thdh,thd_N,power,Freq_THD,thd_data,Freq_Power,PowerS,RubBuzz_data=AUT.analyze_sweep(filename, trigeFrequncy,stopananlysis,channaelselect)
print('FFT Frequency: %.1f Hz' % freq)
print("Sweep Max THD: %.4f%% " %thdh)
print("Sweep Max THD+N: %.4f%% Note, this is single tone use only " %thd_N)
print("spectrum Max Power: %.2fdB " %power)
*1)freq means: single tone , measured frequency *1.1)thd_N means: single Max THD+N *2)thdh means: Sweep Max THD *3)Freq_THD, measure THD's frequency *4)thd_data, measure THD's data *5)Freq_Power,measure Power's frequency *6)PowerS, measure Power's data *7)RubBuzz_data measure RB's data
import LvAut.lvaut_THD as AUT
filename='Device_Mic_THD_R_3.wav'
trigeFrequncy=400 ## this need sweep from high(above 400) to low sweep tone
stopananlysis=100 ## stop analyze_sweep
channaelselect=1 ### if recording is dual channel ,leftchannel=1, rightchannel=2, otherwise no need to define
freq,thdh,thd_N,power,Freq_THD,thd_data,Freq_Power,PowerS,RubBuzz_data=AUT.analyze_sweep(filename, trigeFrequncy,stopananlysis,channaelselect)
AUT.diplaychart(Freq_THD,thd_data,Freq_Power,PowerS,RubBuzz_data,chart_name="save_picture_name",channel= channaelselect)#display chart, pleae note : this function need import matplotlib
analyze sound file spectrogram : Convert a power spectrogram (amplitude squared) to decibel (dB) units This computes the scaling 20 * log10(S / ref)
in a numerically
import LvAut.lvaut_THD as AUT
import LvAut.lvspectrum as lvs
import numpy as np
filename='your soundfile.wav'
y, sample_rate, channels=AUT.load(filename)
S_scale = lvs.stft(y, n_fft=2048, hop_length=512)
Y_scale = np.abs(S_scale)
Y_log_scale = lvs.amplitude_to_db(Y_scale,ref=np.max)
print(Y_log_scale)
## print out all data
import LvAut.lvaut_THD as AUT
import LvAut.lvspectrum as lvs
import LvAut.lvdisplay as lvd
import matplotlib.pyplot as plt
import numpy as np
filename='your sound file.wav'
def plot_spectrogram(Y, sr, hop_length, y_axis="linear"):
plt.figure(figsize=(16, 10))
lvd.specshow(Y,
sr=sr,
hop_length=hop_length,
x_axis="time",
y_axis=y_axis)
plt.colorbar(format="%+2.f")
plt.show()
y, sample_rate, channels=AUT.load(filename)
S_scale = lvs.stft(y, n_fft=1024, hop_length=512)
Y_scale = np.abs(S_scale)
Y_log_scale = lvs.amplitude_to_db(Y_scale,ref=np.max)
plot_spectrogram(Y_log_scale, sample_rate, 512)
github sample code explain https://github.com/Lorrytoolcenter/LvAut
1) masters_speaker_volume.py : test master volume1.1) mic_speaker_level.py : sample for changing current speaker volume and current mic level2) play_sound.py : test play speaker3) record_sound.py : test recording4) plot_spectrogram.py plot spectrogram chart5) spectrumg_data.py : pull out data6) SingleTone_thd.py : play single and get THD7) sweep_thd.py : Play sweep tone and get THD by your input traget tone8) :ref:`<play_record.py>` : recording and play simultaneously9) wav_file_test_sample.py : analyze Sweep WAV file to get FR and THD