Jees 7 сар өмнө
commit
d5d1786205

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+/wave_files/
+/venv/

+ 3 - 0
.idea/.gitignore

@@ -0,0 +1,3 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml

+ 10 - 0
.idea/PuppyVoiceProcessing.iml

@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$">
+      <excludeFolder url="file://$MODULE_DIR$/venv" />
+    </content>
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 14 - 0
.idea/inspectionProfiles/Project_Default.xml

@@ -0,0 +1,14 @@
+<component name="InspectionProjectProfileManager">
+  <profile version="1.0">
+    <option name="myName" value="Project Default" />
+    <inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
+      <option name="ignoredErrors">
+        <list>
+          <option value="N803" />
+          <option value="N802" />
+          <option value="N806" />
+        </list>
+      </option>
+    </inspection_tool>
+  </profile>
+</component>

+ 6 - 0
.idea/inspectionProfiles/profiles_settings.xml

@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>

+ 4 - 0
.idea/misc.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10 (PuppyVoiceProcessing)" project-jdk-type="Python SDK" />
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/PuppyVoiceProcessing.iml" filepath="$PROJECT_DIR$/.idea/PuppyVoiceProcessing.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 31 - 0
main.py

@@ -0,0 +1,31 @@
+# 这是一个示例 Python 脚本。
+
+# 按 Shift+F10 执行或将其替换为您的代码。
+# 按 双击 Shift 在所有地方搜索类、文件、工具窗口、操作和设置。
+from wave_compare import extract_mfcc, compute_similarity
+from datetime import datetime
+import pickle
+
+
+def print_hi(name):
+    # 在下面的代码行中使用断点来调试脚本。
+    print(f'Hi, {name}')  # 按 Ctrl+F8 切换断点。
+
+
+# 按间距中的绿色按钮以运行脚本。
+if __name__ == '__main__':
+    # print_hi('Program Start')
+    print("start time:" + str(datetime.now()))
+    sound_wave_1 = "D://Users//Jees//Documents//coding//PuppyVoiceProcessing//wave_files//puppy4a.wav"
+    sound_wave_2 = "D://Users//Jees//Documents//coding//PuppyVoiceProcessing//wave_files//puppy3b.wav"
+
+    mfcc_1 = extract_mfcc(sound_wave_1)
+    # string_mfcc_1, shape_mfcc_1 = nparray_to_str(mfcc_1)
+    # mfcc_1_reverse = str_to_nparray(string_mfcc_1, shape_mfcc_1)
+    a = pickle.dumps(mfcc_1)
+    b = pickle.loads(a)
+    mfcc_2 = extract_mfcc(sound_wave_2)
+    similarity = compute_similarity(mfcc_2, mfcc_1)
+    print('similarity result:' + str(similarity))
+    print("end time:" + str(datetime.now()))
+# 访问 https://www.jetbrains.com/help/pycharm/ 获取 PyCharm 帮助

+ 28 - 0
wave_compare.py

@@ -0,0 +1,28 @@
+from datetime import datetime
+import librosa
+from matplotlib import pyplot
+from librosa.feature import mfcc
+import numpy as np
+from scipy.spatial.distance import euclidean
+
+
+def extract_mfcc(filename, num_mfcc=13):
+    # 取低频维度上的部分值输出,语音能量大多集中在低频域,数值一般取13。
+    print("start extract_mfcc time:" + filename + str(datetime.now()))
+    audio, sample_rate = librosa.load(filename)
+    # pyplot.figure(figsize=(14, 5))
+    # librosa.display.waveshow(y=audio, sr=sample_rate)
+    # pyplot.title('Wave Form of ' + filename)
+    # pyplot.show()
+    mfcc_result = mfcc(y=audio, sr=sample_rate, n_mfcc=num_mfcc)
+    mean_mfcc = np.mean(mfcc_result, axis=1)
+    print("end extract_mfcc time:" + filename + str(datetime.now()))
+    return mean_mfcc
+
+
+def compute_similarity(feature_1, feature_2):
+    print("start compute_similarity time:" + str(datetime.now()))
+    distance = euclidean(feature_1, feature_2)
+    result = (100-distance)/100
+    print("end compute_similarity time:" + str(datetime.now()))
+    return result