voice-changer/server/sio/MMVC_Namespace.py

39 lines
1.4 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
2023-02-14 23:02:51 +03:00
2022-12-31 10:02:53 +03:00
class MMVC_Namespace(socketio.AsyncNamespace):
2023-02-14 23:02:51 +03:00
def __init__(self, namespace: str, voiceChangerManager: VoiceChangerManager):
2022-12-31 10:02:53 +03:00
super().__init__(namespace)
self.voiceChangerManager = voiceChangerManager
@classmethod
2023-02-14 23:02:51 +03:00
def get_instance(cls, voiceChangerManager: VoiceChangerManager):
2022-12-31 10:02:53 +03:00
if not hasattr(cls, "_instance"):
cls._instance = cls("/test", voiceChangerManager)
return cls._instance
def on_connect(self, sid, environ):
2023-02-17 22:15:34 +03:00
print('[{}] connet sid : {}'.format(datetime.now().strftime('%Y-%m-%d %H:%M:%S'), sid))
2022-12-31 10:02:53 +03:00
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-02-14 23:02:51 +03:00
if (isinstance(data, str)):
print(type(data))
print(data)
2023-01-16 11:59:41 +03:00
await self.emit('response', [timestamp, 0], to=sid)
else:
unpackedData = np.array(struct.unpack('<%sh' % (len(data) // struct.calcsize('<h')), data))
2023-02-20 22:07:43 +03:00
audio1, perf = self.voiceChangerManager.changeVoice(unpackedData)
bin = struct.pack('<%sh' % len(audio1), *audio1)
2023-02-20 22:07:43 +03:00
await self.emit('response', [timestamp, bin, perf], to=sid)
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