Python winsound and Remote HTTP audio output bug

Posting this just in case it helps someone else who runs into a similar issue.

I’m currently routing rhasspy audio output to a windows machine (via Remote HTTP to a simple Flask based API) while doing some testing, and am trying to play the audio that gets posted there on my machine.

Attempting to play the contents of the POST data as bytes directly manages to actually crash Python without any error:

@app.route("/speakers", methods=["POST"])
def speakers():
    winsound.PlaySound(request.data, winsound.SND_MEMORY)
    # The below line exists purely to mock me for my failure
    return {"result": "success"}

If I save the post data off as a WAV file, Python doesn’t crash, but the file still doesn’t play:

@media_center_api_blueprint.route("/speakers", methods=["POST"])
def media_center_speakers():
    tmpdir = tempfile.TemporaryDirectory()
    temp_file_path = os.path.join(tmpdir.name, "test.wav")
    with open(temp_file_path, "wb") as file:
        file.write(request.data)
    winsound.PlaySound(temp_file_path, winsound.SND_FILENAME)
    os.remove(temp_file_path)
    tmpdir.cleanup()
    return {"result": "success"}

I can open that same test.wav file in VLC or seemingly any other audio player without issue, but for whatever reason winsound has issues with it. Have tried multiple Python distributions on multiple machines with the same issue.

Currently using playsound (with one small tweak) instead as a workaround, which is getting the job done for testing purposes at least.