Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a file, cut it, send it...

Status
Not open for further replies.

starn

Programmer
Mar 12, 2002
18
BE
Hi!

I would like to take a wave file for example, read a part of it (for example 64 bits) and send it via a UDP client to a server. I already got and understand the UDP part but I don't know how to open the file, cut it in pieces,...

thanks a lot for any help and sorry for my english...

 
There are various whys to do this but it depends on how you want to approach it.

But you can use fopen(), fread() etc. Or a CFile object from MFC.

HTH
William
Software Engineer
 
Starn - remember that you want to focus on the binary file aspect when following Williamu's advice. The CFile would probably be the easiest one for you to use. This is a nice approach for small files

Basically, what you do is open the file, read your 64 bytes, and send the 64 bytes. If you are dealing with large files, I think you should consider reading the entire wav file in using the binary file - map it to memory - and read from memory at 64 bytes at a time. This is much faster, less disk intensive, etc - but you are also open to memory corruption as well, so you may want to consider checksum usage to validate each 64 bytes.

The best way I can think of is use of a linked list with inherent checksum.
 
WAOW, thanks!

It's the first time I got an answer so fast on this forum...
Actually, it's the first time I got one...eh, two answers!
It's maybe because my last questions were a bit weird or stupid... ;)

Well, yes! At the beginning, I wanted to play a CD on a computer, send the datas with a fast-ethernet network (with UDP) and hear the sound with the second computer. But I think it's maybe easier to start with a sound file already on my PC than to start learning MCI...
In fact,for my final aplication, I would like that the first computer play the right channel and ,the other,the left channel directly from and an AES/EBU input on my sound card for example...( it's 2*24 bits for two sound channels + 2*8 bits of control at 96kHz (AES/EBU (for uncompressed sound))). (So it's not going to be possible to copy the file...I'll need a circular buffer and some other funny things... ;) )

I want to simulate an AES/EBU input with a wave file that I convert into the AES/EBU format and copy the 64 bits samples at 96kHz to a circular buffer and be able to read into this buffer to fill in the ethernet frame before to send it... nice, isn't it? (I hope somebody is going to understand this because my english seems very poor tonight...)


So,if I understand, it's not possible to read a wave file with fread(), it's only for .bin and .txt , as I remember from school...
And, for you,(before I dive into my funky circular buffer...) the best is to read the file with a function that copy it completly in a binary file then read from the .bin each 64 bits with fread().
So, since you know, a little bit more from what I want to do, do you think it's possible to use the CFile class?

If not,what can I use to convert the wave file in a binary file?
Or if you have an other idea for my simulation stuff, any help is really, really welcome!!

Again, sorry for my english and thanks a lot!!

Starn



 
Hi again,

Maybe I've misunderstood but AFAIK there are only two types of file data. Char and Binary. Char files (text) are things like autoexec.bat, readme.txt etc. In other words anything that you can load an read in a std text editor.

All other files are binary, so you shouldn't need to do any conversion on your .wav file.

CoolNamedDenied is right, by far the best approach is to read the entire file into memory. (Remembering of course to free any allocated memory when done.) This will eliminate any playback hiccups you might normally encounter if reading direct from the disk.

HTH




William
Software Engineer
 
Thanks William, now I remember from school...
I found some codes with streams and I think I m going to try this way, but thanks anyway!

Starn


Code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std ;
const int LGMAX = 20 ;

main()
{
   char nomfich [LGMAX+1] ;
   int n ;
   cout << &quot;nom du fichier a lister : &quot; ;
   cin >> setw (LGMAX) >> nomfich ;
   ifstream entree (nomfich, ios::in|ios::binary) ;    // ou ios::in
   if (!entree) { cout << &quot;ouverture impossible \n&quot; ;
                  exit (-1) ;
                }
   while ( entree.read ( (char*)&n, sizeof(int) ) )
        cout << n << &quot;\n&quot; ;
   entree.close () ;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top