Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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... here's my Wave.h file:

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:

 

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 it'll work because first: to replace char I used byte, but if I say:

if(entete.chunkID=="RIFF")

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 here's the exemple of a jpg))

[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
}

I try to write it that way:

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;
	}

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?

Posted

As I suspected, this code is not working I had to change this:

this.nomFichier=nomFichier;
		FileStream stream=new FileStream(nomfichier,FileMode.Open);
		BinaryReader reader=new BinaryReader(stream);
		entete=reader.ReadByte(sizeof(EnTeteJPG));
		reader.Close();
		stream.Close();

 

to this:

this.nomFichier=nomFichier;
		FileStream stream=new FileStream(nomFichier,FileMode.Open);
		BinaryReader reader=new BinaryReader(stream);
		entete=reader.ReadByte(Marshal.SizeOf(EnteteAVI));
		reader.Close();
		stream.Close();

 

because he didnt seem to like me useing SizeOf, the compilator was saying to use Unsafe or marshal.SizeOF

so i chosed Marshal.SizeOf, but now, it tells me the the Marshal.SizeOf is expecting an object and not a class, but that's not how it work in c++...

 

the other error I get are this:

if(entete.chunkId!=Convert.ToByte("RIFF"))
{
return 0;
}

I get the error:

C:\Documents and Settings\Nicolas Dufour\Mes documents\Visual Studio Projects\ZoneMedia\AVI.cs(27): Operator '!=' cannot be applied to operands of type 'byte[]' and 'byte'

 

so as I suspected, he dont see an array of byte as chars... so I dont really know how to handle this here... please anyone can help?

  • Leaders
Posted
Your post was big and it hurt my head. I hope these might help: To convert a string to bytes with one byte per character (like a C++ char), use the System.Text.ASCIIEncoding class. There are tutorials that can be found with google that show how to read/write structs to a file using the FileStream class. This example is in VB but it should be easy to translate to C#.
[sIGPIC]e[/sIGPIC]
Posted
Thank you! and sorry for hurting your head like that, but I wanted my message to be as clear as possible and dont have people asking me: can you post this or post that

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...