123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- import pyaudio
- import wave
- import pymysql
- import time
- from datetime import datetime
- from pydub import AudioSegment
- import uuid
- import os
- os.environ.setdefault("DJANGO_SETTINGS_MODULE", "aiDogProject.settings")
- import django
- django.setup()
- from aiDogApp.models import *
- from .generalRequest import req_url # Importing your generalRequest.py method
- # Audio recording settings
- SAMPLE_RATE = 16000
- BIT_RATE = 16
- CHANNELS = 1
- RECORD_SECONDS = 2
- CHUNK = 1024
- FORMAT = pyaudio.paInt16
- # 连接mysql数据库
- # def get_db_connection():
- # return pymysql.connect(
- # host='127.0.0.1',
- # port=3306,
- # user='root',
- # password='',
- # database='aidog'
- # )
- # 录制音频 并存储为WAV文件
- def record_audio(file_path,mp3_path, duration=4):
- # Audio settings
- sample_rate = 16000
- channels = 1
- bit_rate = pyaudio.paInt16
- chunk_size = 1024
- audio = pyaudio.PyAudio()
- # Start recording
- stream = audio.open(format=bit_rate, channels=channels,
- rate=sample_rate, input=True,
- frames_per_buffer=chunk_size)
- print("Recording...")
- frames = []
- for _ in range(0, int(sample_rate / chunk_size * duration)):
- data = stream.read(chunk_size)
- frames.append(data)
- print("Recording complete.")
- # Stop and close the stream
- stream.stop_stream()
- stream.close()
- audio.terminate()
- # Save audio to file
- with wave.open(file_path, 'wb') as wf:
- wf.setnchannels(channels)
- wf.setsampwidth(audio.get_sample_size(bit_rate))
- wf.setframerate(sample_rate)
- wf.writeframes(b''.join(frames))
- # Convert to MP3
- audio_segment = AudioSegment.from_wav(file_path)
- audio_segment.export(mp3_path, format="mp3")
- # Remove the intermediate WAV file
- os.remove(file_path)
- # Function to insert audio information into MySQL database
- def insert_voice_data(uid, feature_id, wav_file_path,command_type,record_time):
- try:
- # conn = get_db_connection()
- # cursor = conn.cursor()
- # 读取 WAV 文件 为二进制数据
- with open(wav_file_path, 'rb') as wav_file:
- wav_data = wav_file.read()
- # 插入到声纹信息表中
- # 口令类型(0:叫名字 1:坐下,2:站起,3:摇尾巴,4:走过来,5 抬左手,6:抬右手)
- if command_type == 0:
- # 口令类型(0:叫名字
- # insert_query = """
- # INSERT INTO voice_feature_info (uid, voice_feature_id, voice_name_command,command_type)
- # VALUES (%s, %s, %s, %s)
- # """
- # # cursor.execute(insert_query, (uid, feature_id, wav_data, command_type))
- VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
- voice_name_command=wav_data,
- record_time=record_time)
- elif command_type == 1:
- # 口令类型(1:坐下
- VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
- voice_sit_command=wav_data,
- record_time=record_time)
- elif command_type == 2:
- # 口令类型(2:站起
- VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
- voice_stand_command=wav_data,
- record_time=record_time
- )
- elif command_type == 3:
- # 口令类型(3:摇尾巴
- VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
- voice_wagtail_command=wav_data,
- record_time=record_time)
- else:
- # 口令类型(4:走过来
- VoiceFeatureInfo.objects.filter(uid=uid, command_type=command_type).update(voice_feature_id=feature_id,
- voice_come_command=wav_data,
- record_time=record_time)
- # conn.commit()
- print(f"Data inserted into database for UID: {uid}")
- return True
- # 抛出异常
- except Exception as err:
- print(f"Error: {err}")
- return False
- # finally:
- # cursor.close()
- # conn.close()
- file_path = './media/讯飞开放平台.mp3'#读取数据库路径下音源
- APPId = "527f831f"
- APISecret = "YWE5MjI0MzA4NmM3MTNmNTNiMWJkYzE4"
- APIKey = "be5c37f3db1569737934240a0e3ad02d"
- # Main method to record audio, save locally, insert to DB, and call req_url()
- def process_voice_recording(uid,voice_feature_id, command_type):
- # 定义本地缓存路径
- cache_directory = "audio_cache"
- if not os.path.exists(cache_directory):
- os.makedirs(cache_directory)
- # 创建一个唯一的文件名
- timestamp = int(time.time())
- timestamp11 = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
- file_path = os.path.join(cache_directory, f"voice_{uid}_{timestamp}.wav")
- mp3_path=os.path.join(cache_directory, f"voice_{uid}_{timestamp}.mp3")
- # 录制wav音频 并存储到cache
- record_audio(file_path,mp3_path)
- # 插入到数据库 声纹信息表
- if insert_voice_data(uid, voice_feature_id, mp3_path,command_type,timestamp11):
- # 如果插入成功,调用generalRequest.py里的第三方声纹识别API
- res='xxx'
- res=req_url(api_name='createFeature', APPId=APPId,
- APIKey=APIKey, APISecret=APISecret, file_path=mp3_path, featureId=voice_feature_id,groupId=uid) # 传参数 缓存音频路径,声纹id
- print(f"音频文件缓存在 {mp3_path} 并且传参到 req_url().")
- yy = datetime.today()
- end_timestamp = datetime.strftime(yy, '%Y-%m-%d %H:%M:%S')
- # return res
- return (f"音频文件缓存在 {mp3_path} 并且传参到 req_url().返回内容为{res}.请求时间为:---{timestamp11}.返回时间为:---{end_timestamp}")
- else:
- print("Failed to insert voice data into database.")
- return ("Failed to insert voice data into database.")
- # 移除缓存中的wav文件
- # os.remove(file_path)
- # Example Usage:
- # if __name__ == "__main__":
- # user_uid = 'JTYNZP9O' # 用户ID
- # feature_id = 'sfggcvvh1212' # 用户声纹 ID
- # command_type=1
- #
- # process_voice_recording(user_uid, feature_id,command_type)
|