473,466 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

PlaySound() and file length.

93 New Member
Hi,
I'm currently interested in playing a sound in win32 API. I found out that the function PlaySound() does pretty well. However, none of the examples I have seen specify how to use PlaySound with SND_MEMORY, that is, playing a sound that is in the memory. Using the function to open a file and play it is too time consuming (At minimum it took 60ms on my machine.). So, I want to load the sound into memory, and play it using the function. How can I do this? Is there any special way to read in the file? (Like, do I have to throw away the header, etc?). And, if not, how can I get the size of the file to allocate memory for the file? Thank you.
Jun 10 '07 #1
4 3044
weaknessforcats
9,208 Recognized Expert Moderator Expert
You will need to learn how to work with files.

In this case you will want to:

1) open file
2) seek to the end of the file
3) tell to get byte offset from the beginning of the file
4) allocate that much memory
5) read the file to your allocation as binary. This is done in one read.
6) call PlaySound wthe the address of your allocation and the SND_MEMORY.

No guarantee this will be faster that playing the sound from the disc file since in both cases you have to read the file.
Jun 10 '07 #2
DumRat
93 New Member
You will need to learn how to work with files.

In this case you will want to:

1) open file
2) seek to the end of the file
3) tell to get byte offset from the beginning of the file
4) allocate that much memory
5) read the file to your allocation as binary. This is done in one read.
6) call PlaySound wthe the address of your allocation and the SND_MEMORY.

No guarantee this will be faster that playing the sound from the disc file since in both cases you have to read the file.
Actually, I'm trying to use the sounds in a little game. So, I thought of loading all the sounds initially and using those sounds later when I need to. So, it has to be fast. However, after I posted my question, I had an idea, do stuff like that normally happen multithreaded? I mean, I don't know anything about threads, but is it the way it's done? Glad if you can help me out. Thanks.
Jun 10 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
You could load all your sounds as your game starts up and then use the SND_MEMORY during playtime. That will definitely be faster than reading files as needed.

As to multithreading, you could write a function to load your sounds and create a thread using CreateThread. The files are then loaded without interupting the program. It will appear as though it is done in parallel. You have the thread function set an event when the load is complete. Back on the main thread just before you launch into play, you check the event and hold there until the worker thread has finished.

Your thread function must have the form:

Expand|Select|Wrap|Line Numbers
  1. DWORD WINAPI MyThread(LPVOID);
  2.  
Microsoft calls this a threadproc.

You use the address of this function in the CreateThread call.

Good luck.
Jun 10 '07 #4
DumRat
93 New Member
I'll try it out. Thanks.
Jun 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: bballmitch | last post by:
Why won't the following code work? char soundfile2 = "C:/Windows/Media/pinky and the brain.wav"; PlaySound(soundfile2,NULL,SND_FILENAME|SND_SYNC|SND_ASYNC); getchar(); i put libwinmm.a in...
5
by: MLH | last post by:
I have a line of code that works when called from a procedure in Access 2.0 form... PlaySound("C:\cr\help\Help0018.wav", 0) I imported what I thought was needed into A97. However, running it...
3
by: Harry J. Smith | last post by:
I added some sounds to my application, but the example in the msdn Library did not work. It had: public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags ); The...
2
by: Joe Thompson | last post by:
Hi, I am trying to use PlaySound in a VC++.net Windows app (VS 2003). I can use it to play a file but now I want to play it from a resource. I have two questions: How do I add a wav file to...
10
by: Ot | last post by:
I found information on PlaySound. I implemented it in my program and it works just dandy. The only little problem is that I have to package the ..wav files and send them along since PlaySound...
9
by: Andy | last post by:
Hi, I have an application that has several forms. Each form acts as a monitor of a gateway system. If the gateway appears to be dead, the application should play a .wav file (not resource)...
3
by: Jared | last post by:
I'm using the first code sample below to play WAV files stored as embedded resources. For some reason I *occasionally* get scratching and crackling. I'm using a couple WAVs that ship with...
0
by: poppy | last post by:
I tried to play a wav file with function Playsound() but it played only the first seconds of the song.I would like to play whole the song.What might de be wrong?Here is my code in case someone can...
0
by: Mike | last post by:
I am a novice writing a simple program in Visual C++ Express and am having an issue. I basically have several picture boxes of the same size on top of each other with only one visible at a time. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.