open wav with MCI

Just starting out? Need help? Post your questions and find answers here.
Tomio
Enthusiast
Enthusiast
Posts: 291
Joined: Sun Apr 27, 2003 4:54 pm
Location: Germany

open wav with MCI

Post by Tomio »

Hello,

To play a wav-file with the MCI command the open command usually looks like this:
'open sound.wav type waveaudio alias snd'

That is, MCI wants to open the wav file itself.

Does someone know if it is possible (somehow) to give a handle instead of the wav-file path?

Background: I would like to copy several wav-files sound1.wav, sound2.wav,... into one common file, say, allwav.
And instead to extract sound2.wav from allwav into a temporary wav-file to open with MCI it would be a great thing if it would be enough to open allwav once and give the start position of sound2.wav in allwav to MCI.

I hope someone could understand what I mean.

Greetings../tomio
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

EDIT: I think you can use CatchSound(#Sound, MemoryAddress [, Length]) for this, if it doesn't work, read below.

----

You can use a memory pointer. Using file mapping, you could play the required file offset. In this example, I'm using just a memory pointer to a loaded file, but you can easily adapt this to your needs:

Code: Select all

FileName$ = OpenFileRequester("", "", "WAV files|*.wav", 0)
If FileName$
  If ReadFile(0, FileName$)
    WavFileSize = FileSize(FileName$)
    SoundBuffer = AllocateMemory(0, WavFileSize)
    ReadData(SoundBuffer, WavFileSize)
    CloseFile(0)
    If PlaySound_(SoundBuffer, #NULL, #SND_MEMORY|#SND_NODEFAULT|#SND_SYNC)=#FALSE
      Debug "Something went wrong!"
    EndIf
    FreeMemory(0)
  EndIf
EndIf
El_Choni
Tomio
Enthusiast
Enthusiast
Posts: 291
Joined: Sun Apr 27, 2003 4:54 pm
Location: Germany

Post by Tomio »

Hello El_Choni,

yes, this would be a solution. But my WavFiles are big. Really big songs. No chance to allocate memory for that.

../tomio
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: open wav with MCI

Post by PB »

> it would be a great thing if it would be enough to open allwav once and
> give the start position of sound2.wav in allwav to MCI.

Here's a tip from http://www.geocities.com/smigman.geo/mci/wav.html :

Code: Select all

Procedure PlayWavFrom(file$,offset) ; Offset = in milliseconds.
  If Left(file$,1)<>Chr(34) : file$=Chr(34)+file$ : EndIf
  If Right(file$,1)<>Chr(34) : file$+Chr(34) : EndIf
  Debug mciSendString_("open "+file$+" type waveaudio alias MyWav",0,0,0)
  mciSendString_("play MyWav from "+Str(offset),0,0,0)
EndProcedure
;
PlayWavFrom("C:\AudioFile.wav",60000) ; Start playing 60 seconds in.
(Fred: If I remove the Debug command, this doesn't work...?)
Tomio
Enthusiast
Enthusiast
Posts: 291
Joined: Sun Apr 27, 2003 4:54 pm
Location: Germany

Re: open wav with MCI

Post by Tomio »

PB,

this won't help because my allwav file is no longer a wavFile but a concatenation of wavFiles. It can't be opened by MCI.

../tomio
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: open wav with MCI

Post by PB »

> my allwav file is no longer a wavFile but a concatenation of wavFiles

Oh, I see -- I thought you meant you merged all your wavs as one long
one. What you've done is strange -- that's like putting 3 jpgs into one
file and trying to show the middle one. Why are you doing it that way?
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post by El_Choni »

This is an example using file mapping (fill in the values for MapViewOfFile_(). But I wonder if you could do quite the same just using ReadFile(), FileSeek() and ReadData():

Code: Select all

FileName$ = OpenFileRequester("", "", "All files|*.*", 0)
If FileName$
  If ReadFile(0, FileName$)
    hMap = CreateFileMapping_(UseFile(0), #NULL, #PAGE_READONLY, 0, 0, #NULL)
    If hMap
      ; Windows will alocate memory only for the .wav:
      dwFileOffsetLow = ; high-order 32 bits of file offset, 0 in case you don't need it
      dwFileOffsetLow = ; File offset from allwav start (where the .wav to be played starts)
      dwNumberOfBytesToMap = ; .wav's size (not allwav size)
      SoundBuffer = MapViewOfFile_(hMap, #FILE_MAP_READ, dwFileOffsetLow, dwFileOffsetLow, dwNumberOfBytesToMap)
      If SoundBuffer
        If PlaySound_(SoundBuffer, #NULL, #SND_MEMORY|#SND_NODEFAULT|#SND_SYNC)=0:Debug "PlaySound_ failed":EndIf
        UnmapViewOfFile_(SoundBuffer)
      Else
        Debug "MapViewOfFile_ failed"
      EndIf
      CloseHandle_(hMap)
    Else
      Debug "CreateFileMapping_ failed"
    EndIf
    CloseFile(0)
  Else
    Debug "ReadFile failed"
  EndIf
EndIf
End
El_Choni
Tomio
Enthusiast
Enthusiast
Posts: 291
Joined: Sun Apr 27, 2003 4:54 pm
Location: Germany

Re: open wav with MCI

Post by Tomio »

> Why are you doing it that way?

I have a tool which uses many files: txt, bmp and wav.
For protection and more easy to move (to CD or another computer) I have copied all these files together in one (1) big file. The txt+bmp are compressed in addition.
To open and use a file, my tool extracts it from there to memory. But to use a wav I have to extract and copy it to a temporary file before I can use it.

As I already mentioned, the wavs are too big to load into memory.
So, El_Choni's example won't do, because PlaySound want's the wav be loaded into physical mem.
Thank you anyway, El_Choni! Interesting FileMapping. Never heared of it. But good to know.

Frankly, I didn't expect to get a solution. But you never now. I've scanned microsoft + the internet for MCI related things, but no success. Now I think, it's simply not possible.

Greetings../tomio
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

How are you going to play the file if you don't want the file to be loaded into memory?

You say it's because the wav files included in the "archive-file" are so big..!??
Are we talking about 100+ MB wavs?
The only thing I can think of is that you have to do some sort of streaming...(!?!?)

-Lars

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: open wav with MCI

Post by PB »

> For protection and more easy to move (to CD or another computer) I
> have copied all these files together in one (1) big file.

Okay, now I understand: you're doing it like a single resource file, and need
to extract from byte X to byte Y of that single file to disk, for playback.
This would just be a simple matter of taking note where each part of the
single file starts in the big file, and how many bytes it contains, and then
writing it to disk (in a temp folder, hidden, and deleted after playback).
Do you need any help with that? Let me know and I'll post some code...
Tomio
Enthusiast
Enthusiast
Posts: 291
Joined: Sun Apr 27, 2003 4:54 pm
Location: Germany

Post by Tomio »

> How are you going to play the file if you don't want the file to be loaded into memory?
It's a difference to run MCI with data from diskfile or run PlaySound with data from mem.

> Are we talking about 100+ MB wavs?
No, 5-10MB. But at the same time 2x800x600 BMPs are loaded and a soft 32Bit transition takes place.
Ok, I never checked it, but I expect my transition to stumble around if I use mem of that size in addition. The user of my tool should have 60MB mem at least. That's no so much.

../tomio
LarsG
Enthusiast
Enthusiast
Posts: 713
Joined: Mon Jun 02, 2003 1:06 pm
Location: Norway
Contact:

Post by LarsG »

it really shouldn't be a problem having a 10MB wav and two 800x600 bmp loaded in memory... at least.. that's what I think!!! hehe ;)

AMD Athlon XP2400, 512 MB RAM, Hercules 3D Prophet 9600 256MB RAM, WinXP
PIII 800MHz, 320 MB RAM, Nvidia Riva Tnt 2 Mach 64 (32MB), WinXP + Linux
17" iMac, 1.8 GHz G5, 512 MB DDR-RAM, 80 GB HD, 64 MB Geforce FX 5200, SuperDrive, OSX
Tomio
Enthusiast
Enthusiast
Posts: 291
Joined: Sun Apr 27, 2003 4:54 pm
Location: Germany

Re: open wav with MCI

Post by Tomio »

> Do you need any help with that? Let me know and I'll post some code...
Thank's PB, very kindly! But I have all this already and it works. It's the wavfiles which I treat differently.

Next I'll try to load my wavs to memory and check if it's true what LarsG says.
If my image transition will still work without flickering, my posting really was worthwhile!
But it will take some time to modify my tool.

../tomio
Post Reply