Small directx sound problem

The Partridge Family were neither partridges nor a family. Discuss.
User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Small directx sound problem

Post by Asimov » July 9th, 2013, 9:10 pm

Hi Luis,

Well I consider it polite to say Hi before starting a message.
I am 44 btw, and from England, so our customs may differ being from a different country.

I always start a letter starting with dear, even though I am replying to a letter. I consider the electronic form of letters no different to the paper form.

Now I must get back to my tutorial hunting heh heh.
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Small directx sound problem

Post by LuisR14 » July 9th, 2013, 10:36 pm

Asimov wrote: I always start a letter starting with dear, even though I am replying to a letter. I consider the electronic form of letters no different to the paper form.
hehe, yea, it's just i find it funny being repeated (and since i consider forums to be sortof like instant messaging it just feels bothering *ocd kicks in* :lol:)

well, i've kept the topic out-of-topic too long
Asimov wrote:Now I must get back to my tutorial hunting heh heh.
so i'll leave you to that :P
Last edited by LuisR14 on July 10th, 2013, 3:36 am, edited 2 times in total.
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Small directx sound problem

Post by Asimov » July 9th, 2013, 11:25 pm

Hi Luis,

Well I got sound working. Need to perfect it, but it is getting there.
http://www.planetchili.net/forum/viewto ... 8601#p8601

I made chilli's sound routine do looping and all kinds of stuff, so I should be able to work out how to do it here too.
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Small directx sound problem

Post by LuisR14 » July 10th, 2013, 3:35 am

heh, yea, looping was quite easy to implement :P
also, audio has it's own resource type inclusion (which is WAVE ofc lol)
Asimov wrote:I am 44 btw
hehe, twice my age huh :P

(i wonder if i should post this really simple game i made :|)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Small directx sound problem

Post by LuX » July 10th, 2013, 8:53 am

Hi Luis,

Yes, post games, it's always fun to see what people have come up with.
ʕ •ᴥ•ʔ

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Small directx sound problem

Post by Asimov » July 10th, 2013, 5:41 pm

Hi all,

Well I think I have the right idea, but not the way to implement it. Obviously I can play sounds, but the next thing I want to do is to play the sound from resource. I was looking at my text from resource thing, and I think the code will be similar.

Trouble is I know how to load text from a resource, but a wav is binary, so I don't know how to load a binary wav from resource.

Ok this is my WIP and I know it is wrong, cos it still has the text stuff in there instead of loading binary

Code: Select all

HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_LION),L"WAV"); // <- make sure it's the type you specify it to be...
	if (hRes == 0)
       { 
			MessageBox(NULL, LPWSTR(L"WAV Not Loaded"), TEXT("TEXT"), NULL);
        }

   HGLOBAL hMem = LoadResource(NULL, hRes);
   DWORD size = SizeofResource(NULL, hRes);

// this bit needs to be changed so it loads in binary instead of text. No idea atm

   char* resText = (char*)LockResource(hMem);
   char* text = (char*)malloc(size + 1);
   memcpy(text, resText, size);
   text[size] = 0;
	
   void* lockedRes = LockResource(hMem);
Ok this next bit opens the file. I don't need this part, as My resource is already open

Code: Select all

	// Open the wave file in binary.
	error = fopen_s(&filePtr, filename, "rb");
	if(error != 0)
	{
		return false;
	}
 
Now this fread will probably be pointed at my binary data instead. Oh I don't know I am stuck LOL.
When I did it with text LockResource(hMem) is basically an array, but with binary I haven't a clue.

Code: Select all

	// Read in the wave file header.
	count = fread(&waveFileHeader, sizeof(waveFileHeader), 1, filePtr);
	if(count != 1)
	{
		return false;
	}
I wonder if anyone has ideas on this. I am baffled.
I really need to load the WAV from resource as binary.
I think I am on the right track, but I know this code won't work.

Good news is that I can open the file from resource with no errors, so that is a start.
Google is flipping useless. All I get is people showing me how to use playsound grrr.
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Small directx sound problem

Post by LuisR14 » July 11th, 2013, 6:34 am

Asimov wrote:

Code: Select all

HRSRC hRes = FindResource(NULL, MAKEINTRESOURCE(IDR_LION),L"WAV"); // <- make sure it's the type you specify it to be...
hehe, it's actually "WAVE" tho :P
Asimov wrote: Now this fread will probably be pointed at my binary data instead. Oh I don't know I am stuck LOL.
When I did it with text LockResource(hMem) is basically an array, but with binary I haven't a clue.
well when you used LockResource the data is already in binary, you just gotta use memcpy to copy to the WAVE headers you used for loading from file (instead of using the file api functions)

it would just be better to use a pointer allocated to SizeOfResource then just get the data thru LockResource and use pointer arithmetic to access and memcpy the wav data (just like you would for a file), like you have here

Code: Select all

	DWORD size = SizeofResource(NULL, hRes);

	char* resText = (char*)LockResource(hMem);
	
	...memcpy section...
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Small directx sound problem

Post by Asimov » July 11th, 2013, 5:09 pm

Hi Luis,

Code: Select all

hehe, it's actually "WAVE" tho :P
See the attached photo. That is showing showing the resource data, and I used WAV, because that is what the resource folder is. Like I used TXT when I loaded text. Is that right?

Ok I think I am on to something now regardless of WAV or WAVE LOL. I have adjusted my code to this.

Code: Select all

HMODULE hModule = GetModuleHandle(NULL); // get the handle to the current module (the executable file)
HRSRC hResource = FindResource(hModule, MAKEINTRESOURCE(IDR_LION),L"WAV");
if (hResource == 0)
       { 
			MessageBox(NULL, LPWSTR(L"WAV Not Loaded"), TEXT("TEXT"), NULL);
        }
HGLOBAL hMemory = LoadResource(hModule, hResource);
DWORD dwSize = SizeofResource(hModule, hResource);
LPVOID lpAddress = LockResource(hMemory);

char *bytes = new char[dwSize];
memcpy(bytes, lpAddress, dwSize);
Now bytes should be the pointer to the memory address of the WAV.

What I don't know how to do is read the first byte of the memory address from the pointer.
I need to change the following to read from my pointer instead.

Code: Select all

// Read in the wave file header.
	count = fread(&waveFileHeader, sizeof(waveFileHeader), 1, filePtr);
	if(count != 1)
	{
		return false;
	}
Attachments
resource.jpg
resource.jpg (41.87 KiB) Viewed 2904 times
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
Asimov
Posts: 814
Joined: May 19th, 2012, 11:38 pm

Re: Small directx sound problem

Post by Asimov » July 11th, 2013, 5:27 pm

Hi Luis,

Well I have changed the code again. Btw if I change to WAV it doesn't load the resource, so I changed it back. So I know the resource is being loaded, because my error does not come up.

Anyway I changed the code now to this, and it compiles fine, and crashes really well LOL.

Code: Select all

HMODULE hModule = GetModuleHandle(NULL); // get the handle to the current module (the executable file)
HRSRC hResource = FindResource(hModule, MAKEINTRESOURCE(IDR_LION),L"WAV"); // substitute RESOURCE_ID and RESOURCE_TYPE.
if (hResource == 0)
       { 
			MessageBox(NULL, LPWSTR(L"WAV Not Loaded"), TEXT("TEXT"), NULL);
        }
HGLOBAL hMemory = LoadResource(hModule, hResource);
DWORD dwSize = SizeofResource(hModule, hResource);
LPVOID lpAddress = LockResource(hMemory);

char *bytes = new char[dwSize];
memcpy(bytes, lpAddress, dwSize);


	count = fread(&bytes, sizeof(waveFileHeader), 1, filePtr);
	if(count != 1)
	{
		return false;
	}
EDIT:

Doh I know why the above code is not working it is still looking at filePtr which is from a file, instead of looking at my binary which is bytes.

So I think I have to restructure the fread line to read from bytes instead, but I don't know how. Still googling for fread to see if I can make it look at my pointer instead of the file pointer.
----> Asimov
"You know no matter how much I think I have learnt. I always end up hitting brick walls"
http://www.asimoventerprises.co.uk

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Small directx sound problem

Post by LuisR14 » July 11th, 2013, 7:55 pm

Asimov wrote: EDIT:

Doh I know why the above code is not working it is still looking at filePtr which is from a file, instead of looking at my binary which is bytes.

So I think I have to restructure the fread line to read from bytes instead, but I don't know how. Still googling for fread to see if I can make it look at my pointer instead of the file pointer.
yea, hehe, fread (and the other f functions) are for file streams only lol, it won't work with memory (which is what your pointer is)

you would have to do the following instead lol

Code: Select all

LPVOID lpAddress = LockResource(hMemory);

// btw, this isn't necessary, lpAddress already points to all your wavefile data
//char *bytes = new char[dwSize];
//memcpy(bytes, lpAddress, dwSize);

	// instead of this
	//count = fread(&bytes, sizeof(waveFileHeader), 1, filePtr);
	// this lol
	// wavhdr is your waveFileHeader struct variable ofc lol
	memcpy( &wavhdr, lpAddress, sizeof(waveFileHeader) );
	lpAddress += sizeof(waveFileHeader);

	// you can calculate the snd data size from the wavefileheader (or by using some data that is included in the structure of the file, which is better imo)
	memcpy( &<snd data var>, lpAddress, <size of snd data> );
(man i hate phpbb xD)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

Post Reply