Is it possible to receive a sound in RIFF Wave Format using a Socket and Write it to a File and play the sound file

 
   // Load the binary sound file into a byte array
     string result;
     uchar wavHeader[8];
     int file = FileOpen("sound.wav", FILE_WRITE|FILE_READ|FILE_BIN);
     if (file < 0) {Print("Error opening file");}
     uint timeout = 10000;
     uchar response[]; // use an appropriate buffer size
     uint rlen = SocketIsReadable(socket);
     int bytesReceived = SocketRead(socket, response, sizeof(response), timeout);
     result+=CharArrayToString(response,0,bytesReceived);
     Print(result);
     ::FileWriteArray(file, response, 0, bytesReceived);
     ::FileFlush(file);
     ::FileClose(file);
     PlaySound("sound.wav");
 
RofhiT:
   // Load the binary sound file into a byte array
     string result;
     uchar wavHeader[8];
     int file = FileOpen("sound.wav", FILE_WRITE|FILE_READ|FILE_BIN);
     if (file < 0) {Print("Error opening file");}
     uint timeout = 10000;
     uchar response[]; // use an appropriate buffer size
     uint rlen = SocketIsReadable(socket);
     int bytesReceived = SocketRead(socket, response, sizeof(response), timeout);
     result+=CharArrayToString(response,0,bytesReceived);
     Print(result);
     ::FileWriteArray(file, response, 0, bytesReceived);
     ::FileFlush(file);
     ::FileClose(file);
     PlaySound("sound.wav");

For continuous play (stream) or one off ?

 
RofhiT: Is it possible to receive a sound in RIFF Wave Format using a Socket and Write it to a File and play the sound file
  1. Receive and write a file, of course.
  2. Perhaps you should read the manual. PlaySound - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    Only WAV files are played.

    Changing the extension does not change the file type.

 
Lorentzos Roussos #:

For continuous play (stream) or one off ?

One off.
 
RofhiT #:
One off.

The wave file is sent from your phone to a pc terminal on the same network or via the internet ? 

 
William Roeder #:
  1. Receive and write a file, of course.
  2. Perhaps you should read the manual. PlaySound - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    Changing the extension does not change the file type.

to clarify the problem I am sending a sound file in binary from Python to MQL5 using a Socket and when I receive the file its in Riff wave header format and i am trying to write it on a file as a sound.wav and play it in mql5.

refer below for chunk RIFF WAVE format

http://soundfile.sapp.org/doc/WaveFormat/

 
RofhiT:
   // Load the binary sound file into a byte array
     string result;
     uchar wavHeader[8];
     int file = FileOpen("sound.wav", FILE_WRITE|FILE_READ|FILE_BIN);
     if (file < 0) {Print("Error opening file");}
     uint timeout = 10000;
     uchar response[]; // use an appropriate buffer size
     uint rlen = SocketIsReadable(socket);
     int bytesReceived = SocketRead(socket, response, sizeof(response), timeout);
     result+=CharArrayToString(response,0,bytesReceived);
     Print(result);
     ::FileWriteArray(file, response, 0, bytesReceived);
     ::FileFlush(file);
     ::FileClose(file);
     PlaySound("sound.wav");

 

result+=CharArrayToString(response,0,bytesReceived);

Print(result);


When i put 0 i get = RIFFz† 

When i put  8 i get = WAVEfmt  so i want to take all this binary sound and write it to  a file and be able to play it.


result+=CharArrayToString(response,8,bytesReceived);

Print(result);

The canonical WAVE format starts with the RIFF header: 0 4 ChunkID Contains the letters "RIFF" in ASCII form (0x52494646 big-endian form). 4 4 ChunkSize 36 + SubChunk2Size, or more precisely: 4 + (8 + SubChunk1Size) + (8 + SubChunk2Size) This is the size of the rest of the chunk following this number. This is the size of the entire file in bytes minus 8 bytes for the two fields not included in this count: ChunkID and ChunkSize. 8 4 Format Contains the letters "WAVE" (0x57415645 big-endian form). The "WAVE" format consists of two subchunks: "fmt " and "data": The "fmt " subchunk describes the sound data's format: 12 4 Subchunk1ID Contains the letters "fmt " (0x666d7420 big-endian form). 16 4 Subchunk1Size 16 for PCM. This is the size of the rest of the Subchunk which follows this number. 20 2 AudioFormat PCM = 1 (i.e. Linear quantization) Values other than 1 indicate some form of compression. 22 2 NumChannels Mono = 1, Stereo = 2, etc. 24 4 SampleRate 8000, 44100, etc. 28 4 ByteRate == SampleRate * NumChannels * BitsPerSample/8 32 2 BlockAlign == NumChannels * BitsPerSample/8 The number of bytes for one sample including all channels. I wonder what happens when this number isn't an integer? 34 2 BitsPerSample 8 bits = 8, 16 bits = 16, etc. 2 ExtraParamSize if PCM, then doesn't exist X ExtraParams space for extra parameters The "data" subchunk contains the size of the data and the actual sound: 36 4 Subchunk2ID Contains the letters "data" (0x64617461 big-endian form). 40 4 Subchunk2Size == NumSamples * NumChannels * BitsPerSample/8 This is the number of bytes in the data. You can also think of this as the size of the read of the subchunk following this number. 44 * Data The actual sound data.

 

You could also try playing it with the windows MCI , if the sending is handled. Below is an example i copy pasted from another project , it has not been tailored in any way for what you want.

#import "winmm.dll"
int mciSendStringW(string lpszCommand,string lpszReturnString,uint cchReturn,int hwndCallback);
#import
#import "kernel32.dll"
int GetShortPathNameW(string longpath,ushort &result[],int cchBuffer_max_chars_in_result);
#import
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  string path_with_gaps=TerminalPath()+"\\Sounds\\email.wav";
  string dospath=NULL;
  ushort out[];
  int need=GetShortPathNameW(path_with_gaps,out,0);
  ArrayResize(out,need,0);
  int t=GetShortPathNameW(path_with_gaps,out,1024);
  if(t>0){
  dospath=ShortArrayToString(out,0,WHOLE_ARRAY);
  Print("DosPath["+dospath+"]");
  mciSendStringW("play "+dospath+" wait",NULL,0,0);
  //hmm wait command stops and returns to the program after the file finishes
  }else{
  Print("error");
  }
  //mp3 test
  path_with_gaps=TerminalPath()+"\\Sounds\\AlexMoe_Prelude.mp3";
  dospath=NULL;
  ArrayFree(out);
  need=GetShortPathNameW(path_with_gaps,out,0);
  ArrayResize(out,need,0);
  t=GetShortPathNameW(path_with_gaps,out,1024);
  if(t>0){
  dospath=ShortArrayToString(out,0,WHOLE_ARRAY);
  Print("DosPath["+dospath+"]");
  mciSendStringW("open "+dospath+" alias music",NULL,0,0);
  mciSendStringW("play music",NULL,0,0);
  Sleep(10000);
  mciSendStringW("stop music",NULL,0,0);
  mciSendStringW("close music",NULL,0,0);
  }else{
  Print("error : cannot open "+path_with_gaps);
  }
  return(INIT_SUCCEEDED);
  }
 
Lorentzos Roussos #:

The wave file is sent from your phone to a pc terminal on the same network or via the internet ? 

Its sent through a Python Socket connection to MT5


with open('sound.wav', 'rb') as sound_file: total_bytes_sent = 0 file_size = os.path.getsize('sound.wav') data = sound_file.read(1024) conn.sendall(data)

 
Lorentzos Roussos #:

You could also try playing it with the windows MCI , if the sending is handled. Below is an example i copy pasted from another project , it has not been tailored in any way for what you want.

Its a different to what i am trying to do.
 
RofhiT #:
Its a different to what i am trying to do.

Have you saved the file or this is the step you are at ? 

Reason: