Windows 7 Ultimate 64Bit, I am using CodeBlocks 16.01. I am working on my OpenGL college project, I'm trying to add a gun sound effect when the player shoots using the 'spacebar' key.
The sound only works when i create a console project or empty project and the sound plays just fine, I am aware of the library "winmm.lib" that i have to link, and it works just fine as a CONSOLE PROJECT, here is the code for this.
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;
int main()
{
PlaySound("shotgun.wav",NULL,SND_SYNC);
return 0;
}
The problem begins when i now use this code in my Glut Project, what happens is instead of playing "shotgun.wav" it plays one of the windows system sounds called "Default Beep", When the shooting button "spacebar" is pressed. The game was freezing a few seconds until the sound has completed, however i fixed this issue by replacing thw above line with this one :
PlaySound(TEXT("shotgun.wav"), NULL, SND_FILENAME | SND_ASYNC);
It doesn't freeze anymore, But Still, It continues to play that Windows System sound instead of the shotgun.wav. The .wav file is IN the project, i'm pretty sure it's in the right place. here is the sample for shoot code from my Glut project (i only copied the relevant piece of code) :
void Keys(unsigned char key, int x, int y) {
key = tolower(key); //Just in case CAPS LOCK is ON.
if(key==27)
exit(0); //Escape key, Exit the game
switch(key){
case ' ': //SPACE BAR
/* some code here */
PlaySound(TEXT("shotgun.wav"), NULL, SND_FILENAME | SND_ASYNC);
break; //.....Code continues to the next case....
To force the gun sound effect, i had to go to Windows Sound settings and "Change System sounds", lol yep i actually had to replace "Default Beep" with my "Shotgun.wav", i then returned to the game and compiled, and wala ! It was working. BUT of course this is NOT what i want, cause obviously this will only work on my PC and we don't want that.
You need to specify the full file path. Use double slash \\ in between. For example:
sndPlaySound("D:\\New folder(2)\\poiuy\\sound.wav",SND_ASYNC|SND_LOOP);
Related
I've written a small C++ program which checks if Windows clipboard content has changed and prints a type of that content. I compiled the program to .exe file using Windows Visual Studio 2019 and it was blocked by the Windows Defender (file was removed). Why is that happened and how to prevent it?
Of course, if I open the Windows Defender and mark my file as "not a virus" then all works fine, but how to prevent blocking on customers computers? Do I need to create some "manifest" file..?
Sorry if the question is dumb, I'm new in C++ world
#include <iostream>
#include <io.h>
#include <fcntl.h>
#include <Windows.h>
#include <conio.h>
int main()
{
DWORD m_lastClipboardSequenceNumber = GetClipboardSequenceNumber();
while (1) {
Sleep(100);
const DWORD newClipboardSequenceNumber = GetClipboardSequenceNumber();
if (newClipboardSequenceNumber == m_lastClipboardSequenceNumber)
continue;
if (IsClipboardFormatAvailable(CF_UNICODETEXT)) {
std::wcout << "CF_UNICODETEXT\n";
}
if (IsClipboardFormatAvailable(CF_HDROP)) {
std::wcout << "CF_HDROP\n";
}
if (IsClipboardFormatAvailable(CF_BITMAP)) {
std::wcout << "CF_BITMAP\n";
}
m_lastClipboardSequenceNumber = newClipboardSequenceNumber;
}
return 0;
}
Sounds like your issue isn't with C++ at all and more just with Windows, more precisely, Windows Defender. The issue here, to my knowledge, is that Windows Defender started by default not allowing .exe files from unknown sources to be run on the computer without Admin privileges. This is an issue you cannot fix remotely, otherwise that would massively undermine the existing usefulness of Windows Defender, as malicious actors could just use that to run their exploits.
Steps you could take to possibly fix this for your use case: if you have access to the computers you want to run this on, try adding your distribution method to trusted sources. Alternatively, try signing it with a key and adding that signature to trusted.
I personally think since your method for watching clipboard is too abusive, windows defender is blocking your code.
Try monitoring clipboard section and register listeners for clipboard changes to see if same thing happens or not. Your code will be much more complex, since you will need to create a window loop for receiving messages, but I think it will OK that way.
I'm trying to beep, but I simply can't. I've already tried:
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
cout << '\a' << flush;
return 0;
}
I have also tried using this: http://www.johnath.com/beep/
But simply doesn't beep.
(If I run $ speaker-test -t sine -f 500 -l 2 2>&1 on the terminal, it beeps, but I would like to beep with c++ to study low-level sound programming)
And I would like to be able to control frequency and duration.
Unless you're logged in from the console, cout will not refer to the system console. You need to open /dev/console and send the \a there.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int s = open ("/dev/console", O_WRONLY);
if (s < 0)
perror ("unable to open console");
else
{
if (write (s, "\a", 1) != 1)
perror ("unable to beep");
}
}
This depends on terminal emulator are you using. KDE's Konsole, for example, doesn't support beeping with buzzer at all AFAICT. First check that echo -e \\a works in your shell. If it doesn't, your C++ code won't work too. You can use xterm — it does support this.
But even in xterm it may not work if you don't have pcspkr (or snd_pcsp) kernel module loaded. This is often the case when distros blacklist it by default. In this case your bet is looking for a terminal which uses your sound card to emit beeps, not PC speaker AKA buzzer.
You're asking about "low level sound generation", typically, the lowest level of sound generation would involve constructing a wave form and passing it to the audio device in an appropriate format. Of course, then it comes out from your sound card rather than the PC speaker. The best advice I can give would be to read up on the APIs associated with pulse audio, alsa, or even the kernel sound drivers. Last time I played with this (~1996), it basically meant allocating an array of samples, and then computing values to put in there that approximated a sine wave with the appropriate frequency and amplitude, then writing that buffer to the output device. There may have even been some calls to ioctl to set the parameters on the device (sample rate, stereo vs. mono, bit rate, etc). If your audio device supports midi commands, it may be easier to send those in some form that is closer to "play these notes for this long using this instrument".
You may find these articles helpful:
https://jan.newmarch.name/LinuxSound/
http://www.linuxjournal.com/article/6735
Have you tried system call in your application?
system("echo -e '\a'");
First question, sorry if I don't do something right :S. I'm attempting to loop a background audio track while a game created in the console window is played. This is part of a group project. The game runs fine but I simply can't get the audio track to play using the PlaySound() function. This is a test program I made to try to figure out the problem.
#include <iostream>
#include <windows.h>
#include <mmsystem.h>
using namespace std;
int main()
{
PlaySound(TEXT("D:\\CodeBlocks:\\Programming Work:\\SoundTest:\\castor.wav"), NULL, SND_FILENAME|SND_ASYNC|SND_LOOP);
if(PlaySound(TEXT("D:\\CodeBlocks:\\Programming Work:\\SoundTest:\\castor.wav"), NULL, SND_FILENAME|SND_ASYNC|SND_LOOP))
{
cout << "It's Working." << endl;
}
else
{
cout << "It's not working." << endl;
}
cout << "Hello world!" << endl;
return 0;
}
My test case returns true (or "It's working."), and when I tried it in the school computer lab, it would loop the default windows error tone, which plays when the function can't find the file you specified, even though I've given it the whole file path. I can't figure out why it can't find the file, I've quadruple checked that it is in fact located where I wrote the file path, and it still seems unable to find it. I've tried using both .mp3 and .wav formats for the audio file. Anyone know what's going on?
(note: The linker needs to be given the winmm library for this)
Thanks guys, I found the actual problem, it wasn't even code all along. Turns out my audio file (castor.wav) was not actually in wav format, which is required by the PlaySound() function, even though the computer was telling me it was .wav (Even when I showed the properties of the file, it said it was in wav format).
This is because I attempted to convert it from a .mp3 by simply changing .mp3 to .wav, should have known better. After using an actual conversion program (and removing the exact file path and simply giving it TEXT("castor.wav") it works like a charm. Thanks for the help!
Problem:
I need music that repeats/loops in the background while I am running my program. Then, when I get to another part of my program, that background music needs to pause, play another sound file, then resume the background music.
Example Of Problem:
For example, I have a Blackjack program that plays the background music when you run it. Then, when you actually log in or create an account it plays a sound file that says "Welcome". Or if i get to a function that shuffles my vector(deck of cards), it plays a sound effect that sounds like a deck of cards shuffling.
What I Have Tried:
Playsound() - It loops/repeats but does not allow me to pause, resume, etc. My code is:
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib") // Link to the winmm library
PlaySound(TEXT("86876__milton__title-screen.wav"), NULL, SND_LOOP | SND_ASYNC); // Background music
MCI - Can't figure out how to get it to loop/repeat, pause, or resume. All I can get is the following code:
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib") // Link to the winmm library
MCIERROR me = mciSendString("open 86876__milton__title-screen.wav type waveaudio alias song1",NULL, 0, 0);
if (me == 0)
{
me = mciSendString("play song1", NULL, 0, 0);
}
I keep reading that I need to stay away from 3rd party stuff when working with Visual Studio, or anything Microsoft. But, if you can suggest something that is nice and easy, then please.
Other:
I am a beginner so I need it to be as simple as possible. For Python Pygame mixer, it is literally 1 line to play, pause, resume etc. I need something like that!
So I'm learning some stuff in College on C++, and the teacher and I got into a discussion on how to actually center text to the output screen. So my suggestion was to use setw but get the length of the string and the size of the console screen, do the algorithm and BAM we have truly centered text. He says the screen size is 80 but the screen can be resized, which doesn't work no matter what if the output is centered the the user starts resizing. Just a minor question I have, how to get the actual size of the console screen?
#include <iostream>
#include <string>
using namespace std;
const int SCR_SIZE = 80;//some way of telling size
int main(){
string randomText = "Hello User!";
cout << setw( ( (80 / 2) + (randomText.length() / 2 ) ) )
<< randomText
<< endl;
return 0;
}
Searched a little and found this bit
#include <cstdlib>
system("MODE CON COLS=25 LINES=22");
Would that work to set it on execution to make sure my size is what I want it to be? Just read through it so I'm not 100% positive if that in fact is a c++ library
You can #include <windows.h> and call GetConsoleScreenBufferInfo. To use this, you'll need a Windows handle to your standard output stream, which you can retrieve with GetStdHandle.
Be aware that the resulting code will be Windows-specific (whereas your current code is portable, so it should run fine on Linux, Mac OS, *BSD, etc.)
I found out that it's impossible to make the console application window to be bigger than a bit more than half of your screen, like it also is for cmd(command prompt). I'm just warning you that if you can't manually resize it to whatever size you want, you probably can't make it resize. Perhaps you want to try windows application or windows forms application?