I have a C++ code that do this(write from a file directly to memory)
it reads a wave file and store it in memory so it can be changed... heres my Wave.h file:
I reand from the file and write to memory using this code:
I tryed to code something similar in C#, but I dont think itll work because first: to replace char I used byte, but if I say:
will it see the Bytes as an array of char?... here how i trye to do it in c#:
((actually in my c# program I try to read trough all visible media types so heres the exemple of a jpg))
I try to write it that way:
i used various exemple from this site to help me write this code (mainly the marshalling things, which by the way dont work yet....)
so I am close to doing this right? I saw that there is a way to use pointers in c#, maybe I could try with that... anyone can help?
it reads a wave file and store it in memory so it can be changed... heres my Wave.h file:
C#:
struct EnteteWave
{
char chunkID [4];
int chunkSize;
char format [4];
char subchunk1ID [4];
int subchunk1Size;
short audioFormat;
short numChannels;
int sampleRate;
int byteRate;
short blockAlign;
short bitsPerSample;
char subchunk2ID [4];
int subchunk2Size;
};
class FichierWave
{
public:
FichierWave();
~FichierWave();
int LireFichier(char *nomFic);
int EcrireFichier(char *nomFic);
double CalculerDuree();
short GetNbCanaux();//number of chanel
int GetFreqEch();
short GetNbBitsPerEch();
int GetByteRate();
short *donneeG;//left channel data
short *donneeD;//right chanel data
private:
EnteteWave *entete;
int nbSamples;
char *nomFichier;
};
I reand from the file and write to memory using this code:
C#:
int FichierWave::LireFichier(char *nomFic)
{
nomFichier = nomFic;
FILE *fp;
fp = fopen(nomFichier, "rb");
if (! fp)
return 1;
int validation = fread(entete, sizeof(EnteteWave), 1, fp);
if (validation != 1)
return 2;
if (strncmp(entete->chunkID,"RIFF", 4) != 0)
{
fclose(fp);
return 3;
}
if (strncmp(entete->format, "WAVE", 4) != 0)
{
fclose(fp);
return 4;
}
if (strncmp(entete->subchunk1ID, "fmt ", 3) != 0)
{
fclose(fp);
return 5;
}
if (entete->audioFormat != 1)
{
fclose(fp);
return 6;
}
if (strncmp(entete->subchunk2ID, "data",4) != 0)
{
fclose(fp);
return 7;
}
if (entete->subchunk2Size <= 0)
{
fclose(fp);
return 8;
}
...
...
I tryed to code something similar in C#, but I dont think itll work because first: to replace char I used byte, but if I say:
C#:
if(entete.chunkID=="RIFF")
((actually in my c# program I try to read trough all visible media types so heres the exemple of a jpg))
C#:
[StructLayout(LayoutKind.Sequential)]
public struct EnTeteJPG
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte[] SOI;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte[] JFIFMarker;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte[] length;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=5)]
public byte[] identifier;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte[] version;
public byte units;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte[] Xdensity;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=2)]
public byte[] Ydensity;
public byte Xthumbnail;// 0 = no thumbnail
public byte Ythumbnail;// 0 = no thumbnail
}
C#:
public int LireFichier(string nomFichier)
{
this.nomFichier=nomFichier;
FileStream stream=new FileStream(nomfichier,FileMode.Open);
BinaryReader reader=new BinaryReader(stream);
entete=reader.ReadByte(sizeof(EnTeteJPG));
reader.Close();
stream.Close();
if(entete.SOI!=&HFFD8)
{
return 0;
}
if(entete.JFIFMarker!=&HFFE0)
{
return 0;
}
if(entete.identifier!=&H4A46494600)
{
return 0;
}
return 1;
}
so I am close to doing this right? I saw that there is a way to use pointers in c#, maybe I could try with that... anyone can help?