Recommend ways for accepting the wav file format through TCPClient

Hello,

I’m working on a task of sending a wav file from python based JupytherNotebook to the Unity engine.
I didn’t have much experience with socket communication before and was overwhelmed by all kinds of methods on the internet, all the direct introduction is about sending text rather than audio files. There’s also a straightforward method for sending and receiving an audio file in python but the technique is only limited to python. And when I searched on socket communication of files there were a bunch of results on encoding/decoding formats which I think is way too detailed and complicated than I need.

My work is following the problem mentioned in this link . I would appreciate it a lot if someone could point me to some valuable resources and methods on how I should encode for sending on the python side(e.g. UTF8 format) and decode for receiving on the Unity side.

Thanks a lot!

I will assume you can establish the socket and know how to read from it etc (like the code in the link you shared). So your question I assume is how to encode/decode the data you are transmitting. Sockets read and write bytes. If you are dealing with Unicode, to make it portable, it is best to convert the Unicode text to UTF-8 to write over the socket, then decode it at the other end. However. a WAV file is not text, so stay away from UTF-8 encoding.

When sending a binary file, the writing code in the link you provided I assume opens the socket, sends the WAV file contents through, and then I assume closes the socket. This is important because the receiving end needs to know when there is no more data to arrive in the file. If you can open a new socket per file, it is easy - when the socket is closed, there is no more file coming through! But! You can only send one file per socket, and you cannot send the filename.

If you want to send multiple files, with filenames, things get more complicated. I would seriously first consider if you can instead have the python program run “ftp” or similar to copy the file to the machine where Unity is running and have Unity watch for new files that arrive and load them.

If this is not an option, you may need to invent your own “protocol” if you are going to encode it all yourself. For example, you might send one line of text with a file length and file name followed by \n (end of line). The receiving end then reads one line of ASCII text from the socket, extracts the length and filename, then reads that required number of bytes into your WAV file (writing them to disk if the file is big).

But creating your own protocol is a slippery slide - things can go wrong and you have to cope with it all yourself. You might look for a HTTP library for example. Or look at something like Google Protobuf. But its hard to make a good suggestion without knowing how sockets are created, do you need to send multiple files, are the files going to be too big for memory, what other features you will need later, etc.

Personally, I like the simplicity of running an existing file transfer program (like ftp, scp, or even running a web server and using curl to send a file), then just open the file in Unity after its sent across. That leaves all the protocol mumbo-jumbo to the experts.

Otherwise, you can use UnityWebRequestMultimedia.GetAudioClip. You need an HTTP server hosted where the files are that you can communicate with. You can probably do that using a python library and check the example on how to use UnityWebRequestMultimedia.GetAudioClip.