voice-changer/server/sio/MMVC_Namespace.py

36 lines
1.3 KiB
Python
Raw Normal View History

2022-12-31 10:02:53 +03:00
import struct
from datetime import datetime
import numpy as np
import socketio
from voice_changer.VoiceChangerManager import VoiceChangerManager
class MMVC_Namespace(socketio.AsyncNamespace):
def __init__(self, namespace:str, voiceChangerManager:VoiceChangerManager):
super().__init__(namespace)
self.voiceChangerManager = voiceChangerManager
@classmethod
def get_instance(cls, voiceChangerManager:VoiceChangerManager):
if not hasattr(cls, "_instance"):
cls._instance = cls("/test", voiceChangerManager)
return cls._instance
def on_connect(self, sid, environ):
# print('[{}] connet sid : {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S') , sid))
pass
async def on_request_message(self, sid, msg):
2023-01-08 11:58:27 +03:00
timestamp = int(msg[0])
data = msg[1]
2023-01-08 03:45:58 +03:00
unpackedData = np.array(struct.unpack('<%sh' % (len(data) // struct.calcsize('<h')), data))
2023-01-08 11:58:27 +03:00
audio1 = self.voiceChangerManager.changeVoice(unpackedData)
2023-01-04 20:28:36 +03:00
2023-01-05 16:08:26 +03:00
# print("sio result:", len(audio1), audio1.shape)
2023-01-08 11:58:27 +03:00
bin = struct.pack('<%sh' % len(audio1), *audio1)
await self.emit('response', [timestamp, bin])
2022-12-31 10:02:53 +03:00
def on_disconnect(self, sid):
# print('[{}] disconnect'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
pass