Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to msgspec as faster alternative #145

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/requirements-common.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ pydantic-settings
xxhash
aiohttp
certifi
msgpack
msgspec
10 changes: 5 additions & 5 deletions server/restapi/MMVC_Rest_VoiceChanger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import msgpack
from msgspec import msgpack

from fastapi import APIRouter, Request
from fastapi.responses import Response, PlainTextResponse
Expand Down Expand Up @@ -30,7 +30,7 @@ def version(self):
async def test(self, req: Request):
try:
data = await req.body()
timestamp, voice = msgpack.unpackb(data)
timestamp, voice = msgpack.decode(data)

unpackedData = np.frombuffer(voice, dtype=np.int16).astype(np.float32) / 32768

Expand All @@ -40,7 +40,7 @@ async def test(self, req: Request):
if err is not None:
error_code, error_message = err
return Response(
content=msgpack.packb({
content=msgpack.encode({
"error": True,
"timestamp": timestamp,
"details": {
Expand All @@ -52,7 +52,7 @@ async def test(self, req: Request):
)

return Response(
content=msgpack.packb({
content=msgpack.encode({
"error": False,
"timestamp": timestamp,
"audio": out_audio,
Expand All @@ -64,7 +64,7 @@ async def test(self, req: Request):
except Exception as e:
logger.exception(e)
return Response(
content=msgpack.packb({
content=msgpack.encode({
"error": True,
"timestamp": 0,
"details": {
Expand Down
4 changes: 2 additions & 2 deletions server/sio/MMVC_SocketIOServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Literal
from sio.MMVC_Namespace import MMVC_Namespace
from voice_changer.VoiceChangerManager import VoiceChangerManager

from .serializers.msgspec import MsgPackPacket

class MMVC_SocketIOServer:
_instance: socketio.AsyncServer | None = None
Expand All @@ -15,7 +15,7 @@ def get_instance(
allowedOrigins: list[str] | Literal['*'],
):
if cls._instance is None:
sio = socketio.AsyncServer(async_mode="asgi", serializer='msgpack', cors_allowed_origins=allowedOrigins)
sio = socketio.AsyncServer(async_mode="asgi", serializer=MsgPackPacket, cors_allowed_origins=allowedOrigins)
namespace = MMVC_Namespace.get_instance(voiceChangerManager)
sio.register_namespace(namespace)
cls._instance = sio
Expand Down
18 changes: 18 additions & 0 deletions server/sio/serializers/msgspec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import msgspec
from socketio import packet


class MsgPackPacket(packet.Packet):
uses_binary_events = False

def encode(self):
"""Encode the packet for transmission."""
return msgspec.msgpack.encode(self._to_dict())

def decode(self, encoded_packet):
"""Decode a transmitted package."""
decoded = msgspec.msgpack.decode(encoded_packet)
self.packet_type = decoded['type']
self.data = decoded.get('data')
self.id = decoded.get('id')
self.namespace = decoded['nsp']
Loading