Xinonria commited on
Commit
8358abb
1 Parent(s): 6c89c35

标准化响度

Browse files
Files changed (5) hide show
  1. api.py +2 -0
  2. i18n/emotion.json +113 -37
  3. requirements.txt +4 -1
  4. test.py +8 -0
  5. utils.py +41 -0
api.py CHANGED
@@ -2,6 +2,7 @@ import asyncio
2
  import aiohttp
3
  import io
4
  import os
 
5
 
6
  BASE_URL = os.getenv("BASE_URL")
7
  AUDIO_URL = os.getenv("AUDIO_URL")
@@ -16,6 +17,7 @@ async def generate_api(voice_ids, text):
16
  audio_data = await response.read()
17
  # print(type(audio_data))
18
  # 创建一个字节流对象
 
19
  return audio_data
20
  else:
21
  print(response)
 
2
  import aiohttp
3
  import io
4
  import os
5
+ from utils import normalize_audio_loudness
6
 
7
  BASE_URL = os.getenv("BASE_URL")
8
  AUDIO_URL = os.getenv("AUDIO_URL")
 
17
  audio_data = await response.read()
18
  # print(type(audio_data))
19
  # 创建一个字节流对象
20
+ audio_data = normalize_audio_loudness(audio_data)
21
  return audio_data
22
  else:
23
  print(response)
i18n/emotion.json CHANGED
@@ -1,46 +1,122 @@
1
  {
2
  "zh": {
3
- "正常": "正常",
4
- "生气": "生气",
5
- "悲伤": "悲伤",
6
- "惊讶": "惊讶",
7
- "害怕": "害怕",
8
- "厌恶": "厌恶",
9
- "开心": "开心",
10
- "失望": "失望",
11
- "紧张": "紧张"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  },
13
  "en": {
14
- "正常": "normal",
15
- "生气": "angry",
16
- "悲伤": "sad",
17
- "惊讶": "surprise",
18
- "害怕": "fear",
19
- "厌恶": "disgust",
20
- "开心": "happy",
21
- "失望": "disappointment",
22
- "紧张": "nervous"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  },
24
  "ja": {
25
- "正常": "正常",
26
- "生气": "怒り",
27
- "悲伤": "悲しい",
28
- "惊讶": "驚き",
29
- "害怕": "恐れ",
30
- "厌恶": "嫌悪",
31
- "开心": "嬉しい",
32
- "失望": "失望",
33
- "紧张": "緊張"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  },
35
  "ko": {
36
- "正常": "보통",
37
- "生气": "화남",
38
- "悲伤": "슬픔",
39
- "惊讶": "놀람",
40
- "害怕": "두려움",
41
- "厌恶": "혐오",
42
- "开心": "행복",
43
- "失望": "실망",
44
- "紧张": "긴장"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  }
46
- }
 
1
  {
2
  "zh": {
3
+ "正常": "正常",
4
+ "生气": "生气",
5
+ "悲伤": "悲伤",
6
+ "惊讶": "惊讶",
7
+ "害怕": "害怕",
8
+ "厌恶": "厌恶",
9
+ "开心": "开心",
10
+ "失望": "失望",
11
+ "紧张": "紧张",
12
+ "不屑": "不屑",
13
+ "不甘": "不甘",
14
+ "愉悦": "愉悦",
15
+ "戏谑": "戏谑",
16
+ "疑惑": "疑惑",
17
+ "尴尬": "尴尬",
18
+ "伤心": "伤心",
19
+ "困倦": "困倦",
20
+ "无奈": "无奈",
21
+ "警惕": "警惕",
22
+ "坚决": "坚决",
23
+ "委屈": "委屈",
24
+ "害羞": "害羞",
25
+ "傲娇": "傲娇",
26
+ "女主": "女主",
27
+ "平淡": "平淡",
28
+ "疑问": "疑问",
29
+ "男主": "男主",
30
+ "警惕 ": "警惕 "
31
  },
32
  "en": {
33
+ "正常": "normal",
34
+ "生气": "angry",
35
+ "悲伤": "sad",
36
+ "惊讶": "surprise",
37
+ "害怕": "fear",
38
+ "厌恶": "disgust",
39
+ "开心": "happy",
40
+ "失望": "disappointment",
41
+ "紧张": "nervous",
42
+ "女主": "female",
43
+ "平淡": "indifferent",
44
+ "疑问": "question",
45
+ "戏谑": "playful",
46
+ "困倦": "sleepy",
47
+ "男主": "male",
48
+ "疑惑": "puzzled",
49
+ "无奈": "helpless",
50
+ "不甘": "unwilling",
51
+ "傲娇": "tsundere",
52
+ "警惕": "vigilant",
53
+ "害羞": "shy",
54
+ "警惕 ": "vigilant ",
55
+ "尴尬": "embarrassed",
56
+ "坚决": "determined",
57
+ "不屑": "disdainful",
58
+ "愉悦": "joyful",
59
+ "委屈": "aggrieved",
60
+ "伤心": "heartbroken"
61
  },
62
  "ja": {
63
+ "正常": "正常",
64
+ "生气": "怒り",
65
+ "悲伤": "悲しい",
66
+ "惊讶": "驚き",
67
+ "害怕": "恐れ",
68
+ "厌恶": "嫌悪",
69
+ "开心": "嬉しい",
70
+ "失望": "失望",
71
+ "紧张": "緊張",
72
+ "女主": "ヒロイン",
73
+ "平淡": "平淡",
74
+ "疑问": "疑問",
75
+ "戏谑": "からかい",
76
+ "困倦": "眠い",
77
+ "男主": "主人公",
78
+ "疑惑": "困惑",
79
+ "无奈": "仕方ない",
80
+ "不甘": "悔しい",
81
+ "傲娇": "ツンデレ",
82
+ "警惕": "警戒",
83
+ "害羞": "恥ずかしい",
84
+ "警惕 ": "警戒 ",
85
+ "尴尬": "気まずい",
86
+ "坚决": "断固とした",
87
+ "不屑": "軽蔑",
88
+ "愉悦": "喜び",
89
+ "委屈": "悔しい",
90
+ "伤心": "悲しい"
91
  },
92
  "ko": {
93
+ "正常": "보통",
94
+ "生气": "화남",
95
+ "悲伤": "슬픔",
96
+ "惊讶": "놀람",
97
+ "害怕": "두려움",
98
+ "厌恶": "혐오",
99
+ "开心": "행복",
100
+ "失望": "실망",
101
+ "紧张": "긴장",
102
+ "女主": "여주인공",
103
+ "平淡": "평범함",
104
+ "疑问": "의문",
105
+ "戏谑": "농담",
106
+ "困倦": "피곤함",
107
+ "男主": "남주인공",
108
+ "疑惑": "의혹",
109
+ "无奈": "어쩔 수 없음",
110
+ "不甘": "불만",
111
+ "傲娇": "츤데레",
112
+ "警惕": "경계",
113
+ "害羞": "부끄러움",
114
+ "警惕 ": "경계 ",
115
+ "尴尬": "당황",
116
+ "坚决": "단호함",
117
+ "不屑": "경멸",
118
+ "愉悦": "즐거움",
119
+ "委屈": "억울함",
120
+ "伤心": "상심"
121
  }
122
+ }
requirements.txt CHANGED
@@ -1,4 +1,7 @@
1
  gradio
2
  aiohttp
3
  pypinyin
4
- gradio-i18n==0.0.10
 
 
 
 
1
  gradio
2
  aiohttp
3
  pypinyin
4
+ gradio-i18n==0.0.10
5
+ pyloudnorm
6
+ soundfile
7
+ pydub
test.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ from main import demo
2
+
3
+ if __name__ == "__main__":
4
+ demo.launch(
5
+ server_port=8080,
6
+ show_api=False,
7
+ share=True
8
+ )
utils.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import os
3
+ import pickle
4
+
5
+ import soundfile as sf
6
+ import numpy as np
7
+ from pydub import AudioSegment
8
+ from pyloudnorm import Meter
9
+
10
+ os.chdir(os.path.dirname(os.path.abspath(__file__)))
11
+
12
+ def normalize_audio_loudness(data: bytes, target_loudness: float = -23.0) -> bytes:
13
+ audio = AudioSegment.from_file(io.BytesIO(data), format='mp3')
14
+ meter = Meter(audio.frame_rate) # 创建一个响度计量器
15
+ sr = audio.frame_rate
16
+ samples = audio.get_array_of_samples()
17
+ audio = np.array(samples, dtype=np.float64)
18
+
19
+ # 测量积分响度
20
+ loudness = meter.integrated_loudness(audio)
21
+
22
+ # 计算增益
23
+ gain_db = target_loudness - loudness
24
+ gain_linear = 10 ** (gain_db / 20.0)
25
+
26
+ # 应用增益
27
+ balanced_audio = audio * gain_linear
28
+
29
+ # 应用软限幅以防止削波
30
+ balanced_audio = np.tanh(balanced_audio)
31
+
32
+ # 将numpy数组转换回bytes
33
+ balanced_audio = (balanced_audio * 32767).astype(np.int16)
34
+ byte_io = io.BytesIO()
35
+ sf.write(byte_io, balanced_audio, sr, format='mp3')
36
+ normalized_audio_bytes = byte_io.getvalue()
37
+
38
+ return normalized_audio_bytes
39
+
40
+ if __name__ == "__main__":
41
+ normalize_audio_loudness()