c++ - Converting from C: fputc and fwrite in C#? -


question: in order write c# interface libespeak, need convert callback synthcallback c#.

see below c code.
might need reference:
https://ccrma.stanford.edu/courses/422/projects/waveformat/
http://www-mmsp.ece.mcgill.ca/documents/audioformats/wave/wave.html

basically,

espeak_initialize espeak_setsynthcallback(synthcallback); espeak_setparameter(espeakrate, 510, 0);         espeak_synth(".", 20, 0, pos_character, 0, 0, null, null); 

are dllimport-ed function. , have them working asynchronously, without file.

now want synchronous version working files, have little problem:

the callback function

static int synthcallback(short *wav, int numsamples, espeak_event *events) 

first, need create delegate can pass function c dll/so. isn't problem, if make short *wav system.intptr, how write data file ?

in other words: can me fwrite, fputc, write4bytes converting proper c#?

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <espeak/speak_lib.h>  // gcc -o mine speak.cpp -i/usr/include/espeak/ -lespeak   file *f_wavfile = null;  static int synthcallback(short *wav, int numsamples, espeak_event *events);   // write 4 bytes file, least significant first static void write4bytes(file *f, int value) {     int ix;      for(ix=0; ix<4; ix++)     {         fputc(value & 0xff,f);         value = value >> 8;     } }    int openwavfile(char *path, int rate) {     static unsigned char wave_hdr[44] = {         'r','i','f','f',0x24,0xf0,0xff,0x7f,'w','a','v','e','f','m','t',' ',         0x10,0,0,0,1,0,1,0,  9,0x3d,0,0,0x12,0x7a,0,0,         2,0,0x10,0,'d','a','t','a',  0x00,0xf0,0xff,0x7f     };       if(path == null)         return(2);      if(path[0] == 0)         return(0);      if(strcmp(path,"stdout")==0)         f_wavfile = stdout;     else         f_wavfile = fopen(path,"wb");      if(f_wavfile == null)     {         fprintf(stderr,"can't write to: '%s'\n",path);         return(1);     }       fwrite(wave_hdr, 1, 24, f_wavfile);     write4bytes(f_wavfile, rate);     write4bytes(f_wavfile, rate * 2);     fwrite(&wave_hdr[32], 1, 12, f_wavfile);     return(0); }   //  end of openwavfile    static void closewavfile() {     unsigned int pos;      if((f_wavfile==null) || (f_wavfile == stdout))         return;      fflush(f_wavfile);     pos = ftell(f_wavfile);      fseek(f_wavfile, 4, seek_set);     write4bytes(f_wavfile, pos - 8);      fseek(f_wavfile, 40, seek_set);     write4bytes(f_wavfile, pos - 44);      fclose(f_wavfile); } // end of closewavfile   int main()  {        char buf[22050];     int = 0;     memset(&buf, 0, sizeof(buf));     // openwavfile((char*) "test.wav", 22050);     int samplerate = espeak_initialize(audio_output_synchronous, 300, null, 0);      openwavfile((char*) "test.wav", samplerate);      espeak_setsynthcallback(synthcallback);     //espeak_setparameter(espeakrate, 510, 0);     //espeak_setparameter(espeakrange, 75, 0);     (i=0; < 9;i++)      {         /*         espeak_error espeak_synth(             const void *text,             size_t size,             unsigned int position,             espeak_position_type position_type,             unsigned int end_position,             unsigned int flags,             unsigned int* unique_identifier,             void* user_data);         */         //espeak_position_type.pos_character         espeak_synth("test", 10, 0, pos_character, 0, 0, null, null);         fwrite(buf, 1, 5512, f_wavfile);         espeak_synth(".", 20, 0, pos_character, 0, 0, null, null);         fwrite(buf, 1, 22050, f_wavfile);     }     closewavfile(); }   static int synthcallback(short *wav, int numsamples, espeak_event *events) {     if (wav == null)          return 0;      if (numsamples > 0)      {         fwrite(wav, numsamples * 2, 1, f_wavfile);     }      return 0; } 

filestream sink; int32 synthcallback(intptr wav, int32 numsamples, intptr events) {     var wavm = new int16[numsamples];     marshal.copy(wav, wavm, 0, numsamples);     // , wavm      //or     var wavbytes = new byte[numsamples * 2];     marshal.copy(wav, wavbytes, 0, numsamples*2);     sink.write(wavbytes, 0, numsamples*2); } 

for writing 32-integers, can either use bitconverter.getbytes , filestream.write or maybe binarywriter.


Comments

Popular posts from this blog

c++ - Is it possible to compile a VST on linux? -

java - Output of Eclipse is rubbish -

jquery - Confused with JSON data and normal data in Django ajax request -