Re: Can I use a byte array for sound files?

From: Gordon Burditt (gordonb.380zy_at_burditt.org)
Date: 06/04/04


Date: 4 Jun 2004 16:27:55 GMT

Sound and sockets are off-topic in comp.lang.c but there are some
issues here that are on-topic.

>I am using MCI (winmm.dll) to read, record and playback sound. For
>now, I am doing this with disk files instead of realtime doing it
>straight from the memory. If I want to stream/relay/transmit this
>sound (file) on a Windows socket (not using FTP but TCP), can I read
>the file into a byte array?

You can read *ANY* readable file into a byte array (well, in C it's
called a char array) no matter what its format. What you do with
it after that is up to you.

>Because if it is possible then I can send
>it on the socket but the problems I foresee are:
>
>How will I repack it into the file. Will just reading back the byte
>array into a file with the same extention work?

Reading the file into a char array, then writing it back should
work to make a copy of the file, barring problems like running out
of disk space, permissions, bad sectors, etc. The extension is not
relevant to C. Nor is the fact that this file allegedly contains
sound.

>etc.

>I mean, say, I have a
>.wav file and I read that into a byte array. Then I prefix some header
>of my own containing the file name, the type and size etc. and send
>this message on a socket, then at the other end, I read this header
>and the following byte array and read it back into a file with the
>same name and extention, will it be okay or it'll just be
>gobbledegook?

What's the format expected for the file? (Does the file have fields
in it that are byte-order-sensitive, like, say, a 24-bit field for
sampling frequency?) What's the format expected over the socket?
If you add a header before sending it you should be prepared to
remove it before writing the file. Treating the file as a byte
array shouldn't add any MORE platform-dependencies than there were
in the original file.

When transferring files over sockets (also true of reading blocks
of data from the keyboard, and disk files, but it may be a bit more
erratic with sockets): fread() may return chunks of data that are
not the same size as the chunks you wrote. (e.g. you wrote two
500-byte blocks; you might get them back as three 300-byte blocks
and one 100-byte block.) Be prepared to handle this situation.
*USE* the amount of data read that fread() returns, don't assume
you got all you asked for, and don't assume that if you got a short
block that it's the last one.

>How do they stream sound on the Internet otherwise?

I don't know what format they use for streaming sound, but
you probably have to stream licenses at the same time, and
remove the CSS encryption.

                                        Gordon L. Burditt



Relevant Pages

  • Re: socket programming
    ... > using sockets we exchange text between endpoints of the socket but ... > first we convert the text to array of bytes and we send it to the other ... For sending a binary integer to an endpoint, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: File transfer
    ... byte array of that size, ... Ciaran O''Donnell wrote: ... through sockets. ... the receivers end. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: convert fd to IO object in extension?
    ... >>network sockets and disk file I/O. ... Caller creates an instance of Epoll ... add the IO object to a Hash and also call epoll_ctlto add ... that returns, builds an array of 2-cell arrays, the first cell containing ...
    (comp.lang.ruby)
  • Re: size limit for byte array?
    ... Sockets are streaming in nature, and while you might create a 16KB byte ... array on one end and send off using 1 statement, ... Instead of breaking the loop on "less than full buffer", ... on "empty buffer". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: lessons learned on Black Knight speech problems
    ... so that and some new sockets should fix things. ... >> have two Black Knights with one game being the good game, ... >> the bad boards and see if the sound behaves correctly. ... quickly swapped the cables to the right position and the ...
    (rec.games.pinball)