VC++ play sound - visual-c++

I need C++ code to play sound at particular decibels that are choose by user dynamically.

This was answered Here
You need to use the absolute path, make sure that you're sending a filename (use SND_FILENAME flag), and pause the program long enough to play the sound file (e.g., use getchar()). You need to link the winmm.lib library in your project settings, and #include windows.h and mmsystem.h in the header.
#include <windows.h>
#include <mmsystem.h>
int main() {
PlaySound((LPCSTR) "C:\\kenny g.WAV", NULL, SND_FILENAME | SND_ASYNC);
getchar();
}

Related

Inquiry: Integrating libsndfile with Visual Studio 2010 C++. Error: libsndfile.dll not found

I am teaching myself how to read in wav files into C++ as a part of me learning C++. I have found many resources online that recommended the following library: libsnfile library
So I followed some tutorials below in testing the basic functionality of the library, but I can't get the library to compile with Visual Studio 2010.
I have searched online for the following error, but did not find anything useful for my particular error. I downloaded the libsndfile C++ windows installer found here. I used the 32bit version since I am using the win32 C++ console version. However, my Visual Studio is 64 bit. I did the following after I downloaded the installer:
I went into Visual Studio. Under my project, I did the following:
In project properties:
1. VC++
Include >> added ...\libsnfile\include
Library >> added ...\libsnfile\lib
2. C\C++
Added the following directory as additional dependencies
...\libsnfile\lib\libsndfile-1.lib
I did this to add this third party library to my project. After this, to test, I ran the following code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int _tmain(int argc, _TCHAR* argv[])
{
printf("This is a test\n");
getchar();
return 0;
}
I coded that to make sure that I could access the sndfile.h in my program and everything compiled. The problem occured when I tried to implement the following code:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <sndfile.h>
int _tmain(int argc, _TCHAR* argv[])
{
printf("This is a test\n");
//This will be the length of the buffer used to hold samples while the program processes them.
//A SNDFILE is like FILE in a standard C library. Consequently, the sf_open_read and sf_open_write functions will return an
//SNDFILE* pointer when they successfully open the specified file.
SNDFILE* sf = NULL;
/*SF_INFO will obtain information of the file we wish to load into our program. */
SF_INFO info;
/*This is where the program will open the WAV file */
info.format = 0;
sf = sf_open("C:\Users\GeekyOmega\Desktop\gameon.wav", SFM_READ, &info);
if(sf == NULL)
{
printf("Failed to open the file.\n");
exit(-1);
}
getchar();
return 0;
}
I then get a system error when I click run inside visual studio when I try to run my program. It says,
The program can't start because libsnfile-1.dll is missing from your computer.
Try reinstalling the program to fix this problem.`
I tried the 64 bit windows installer and tried that, but it didn't work. Anyone understand what I am doing run? I am running Visual Studio's 2010 on Windows 7 as my dev environment.
I apologize if I am making a silly mistake, but I would deeply appreciate if anyone could help me. I tried a few hacky fixes, as I talked about above, but nothing has worked.
EDIT: I am also aware of this thread here, but this doesn't make any sense to my current issue as I am not doing any of this path stuff that they are talking about.
Warm Regards,
GeekyOmega
I fixed the issue. For future readers, this is a very common problem, I think. I placed the .dll in the debug folder of my Visual Studio project. Visual Studio couldn't see the .dll file otherwise. After this, the program fired up as expected and ran. If this does not fix the issue for you, then I suggest something else could be going on.

How to read the contents of a directory during a custom system call in Linux kernel 3.2.x

I'm trying to open a directory during a custom system call in the Linux kernel (3.2.17) using openDir:
#include <linux/kernel.h>
#include <linux/unistd.h>
#include <linux/types.h> // also tried "asm/types.h"
#include <linux/dirent.h>
#include <linux/stat.h>
asmlinkage
int sys_mycall( const char* srcDir ) {
DIR* dir_p;
struct dirent *dirEntry;
struct stat inode;
dir_p = opendir(srcDir);
...
...
}
However, the compiler can't find what it needs
mycall.c:9:5: error: unknown type name ‘DIR’
mycall.c:14:5: error: implicit declaration of function ‘opendir’ [-Werror=implicit-function-declaration]
If this was a user space application, then I would #include <dirent.h> and <sys/types.h>, but I don't have these available. The compiler doesn't seem to have any trouble finding the above headers, but it's obviously not getting what it need.
Is it possible to make this call from another syscall?
I see in another related question that someone suggested implementing or reusing what the desired syscall is doing under the hood, as opposed to calling it directly.
If it's possible, could someone let me know what I need to make the call (again, this is Linux 3.2.17).
Thanks.
Edit:
This seems to be the way to go: How do I open a directory at kernel level using the file descriptor for that directory?
Yes, it is possible to make system call from another system call. But it has limitation.
Also you cannot used opendir() in kernel.
Try to look for struct nameidata nd
use user_path_parent();
Check this link: http://lxr.free-electrons.com/source/fs/namei.c#L3352

Question about CImage save function

I have encountered a problem while saving the CImage class data. The following are my brief set up:
#include "stdafx.h"
#include <cstring>
#include <atlimage.h>
#include <vector>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
CImage myimage;
myimage.Create(100,100,24);
// save an image in BMP format
string impath = "image1.bmp";
myimage.Save((LPCTSTR)impath.c_str());
return 0;
}
I have run it as a WIN32 console application under VC++ 2008. There is no error. However, after the program done, there is NO file created under my directory. I have run this code on two machines, and the results are the same.
Thank you very much for your time and effort.
Maybe you are just looking in the wrong place for the file. Give an absolute path. The relative path is to the current working directory of the process. If you run from the debugger, it could be the output directory of the executable, the project directory or somewhere else.
Also, instead of the cast use the _T macro on c_str() -- in case it needs a conversion.
If you still can't figure it out, get PROCMON (free utility from Microsoft). Run it and filter the output so it's just looking for file with path contains "image1.bmp" -- it will tell you all file operations that were tried with that path and what happened.
myimage.Save((LPCTSTR)impath.c_str());
1) check result
2) why did you use type casting? this can mask the bug, e.g. if you use UNICODE build (default one in VS2008), filename will be invalid

How do I program for Linux's new `fanotify` file system monitoring feature?

fanotify, built on top of fsnotify, is supposed to replace inotify which replaced dnotify. Are there some good programming examples or existing utilities that use fanotify to watch for changes in a filesystem? How much detail does fanotify provide?
This LWN article is often quoted as a source of documentation for fanotify. But the description there appears to be out of date. fanotify no longer works using a socket connection. Instead, there are two new libc functions wrapping syscalls, declared in sys/fanotify.h. One is called fanotify_init, the other is fanotify_mark. At the time of this writing, these syscalls are still included in the list of missing manual pages. There is, however, a mail containing drafts for these manual pages. With a combination of these man pages, a look at the headers in question, and a bit of trial and error, you should be able to get this going.
It seems that some of the functionality originally envisioned for fanotify is no longer suipported in that fashion. For example, the LWN article describes a FAN_GLOBAL_LISTENER flag which will implicitely mark the whole filesystem tree unless parts are explicitely unmarked. The current interface has no such provision, but a similar result can be achieved using the following mark:
fanotify_mark(fan,
FAN_MARK_ADD | FAN_MARK_MOUNT,
FAN_OPEN | FAN_EVENT_ON_CHILD,
AT_FDCWD, "/")
Where inotify events provide the path to the accessed object as part of the event, fanotify opens a file descriptor for it. In order to turn this descriptor into a path name, the corresponding entry from the proc file system can be used, as described here.
Here is a simple example which simply prints the name of every opened file:
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/fanotify.h>
#include <sys/stat.h>
#include <sys/types.h>
#define CHK(expr, errcode) if((expr)==errcode) perror(#expr), exit(EXIT_FAILURE)
int main(int argc, char** argv) {
int fan;
char buf[4096];
char fdpath[32];
char path[PATH_MAX + 1];
ssize_t buflen, linklen;
struct fanotify_event_metadata *metadata;
CHK(fan = fanotify_init(FAN_CLASS_NOTIF, O_RDONLY), -1);
CHK(fanotify_mark(fan, FAN_MARK_ADD | FAN_MARK_MOUNT,
FAN_OPEN | FAN_EVENT_ON_CHILD, AT_FDCWD, "/"), -1);
for (;;) {
CHK(buflen = read(fan, buf, sizeof(buf)), -1);
metadata = (struct fanotify_event_metadata*)&buf;
while(FAN_EVENT_OK(metadata, buflen)) {
if (metadata->mask & FAN_Q_OVERFLOW) {
printf("Queue overflow!\n");
continue;
}
sprintf(fdpath, "/proc/self/fd/%d", metadata->fd);
CHK(linklen = readlink(fdpath, path, sizeof(path) - 1), -1);
path[linklen] = '\0';
printf("%s opened by process %d.\n", path, (int)metadata->pid);
close(metadata->fd);
metadata = FAN_EVENT_NEXT(metadata, buflen);
}
}
}
The documentation for the fanotify API is available in the Linux manpages:
fanotify.7 - http://man7.org/linux/man-pages/man7/fanotify.7.html
fanotify_init.2 - http://man7.org/linux/man-pages/man2/fanotify_init.2.html
fanotify_mark.2 - http://man7.org/linux/man-pages/man2/fanotify_mark.2.html
Here are some examples, fatrace being the most elaborate.
https://launchpad.net/fatrace
http://git.infradead.org/users/eparis/fanotify-example.git
http://www.lanedo.com/~aleksander/fanotify/fanotify-example.c
Bindings exist for Go and Python.
I just learned about fanotify and it seems very nice. Very nice interface!
It is not in the Linus tree yet but I guess it will get there for Linux 2.6.33 and before for testing (I noticed some patches today in LKML). In the original patch they announce a GIT tree thus you might be able to build a testing kernel from there. You might also find testing git trees.
I couldn't find utilities that use it but I guess they'll come soon.
There is an example here, at the end of the email:
http://lwn.net/Articles/339253/
If you are really interested in this new feature you might want to monitor the Linux Kernel Mailing List and interact there. You can also wait until the utilities are released or develop your own.
About the detail, it seems fanotify provides less events than inotify. I guess this might change in the future but since this is a brand new feature in development there is not much I can say about it now.

can't get my code to run from a programming book(c++)

i got a new programing book (multicore programming by cameron hughes, tracey hughes).
so far i have not got one of their programs to work their book says that it should work on 99% of computers so im a little confused but at the end of each program in their book they have "compile and link instructions"... do i need to enter that? it looks something like this "C++ -o guess_it guess_it.cc". the code im runnning right now is:
#include <iostream>
#include <windows.h>
#include <string>
#include <spawn.h>
#include <sys/wait.h>
using namespace std;
int main(int argc,char *argv[],char *envp[])
{
pid_t ChildProcess;
pid_t ChildProcess2;
int RetCode1;
int RetCode2;
int Value;
RetCode1 = posix_spawn(&ChildProcess,"find_code",NULL,
NULL,argv,envp);
RetCode2 = posix_spawn(&ChildProcess2,"find_code",NULL,
NULL,argv,envp);
wait(&Value);
wait(&Value);
return(0);
}
im running windows 7(32-bit), AMD athion x2 7550 dual-core proessor, VC++ 2008 Express edition.
i get the following error : fatal error C1083: Cannot open include file: 'spawn.h': No such file or directory
anyone know why i can't get my code to run? do i need to download something? because i read the book and did not see anything about downloading anything but i might be wrong. :(
It looks like that book is using POSIX threading. Visual Studio uses Windows Threading by default, which has a completely different API.
You most likely just need to get a copy of a POSIX Thread library for Windows. That will include spawn.h and the appropriate lib files for you to use.
Forgive me if I'm misreading your level of experience here, but it sounds as though you are a complete beginner with this language.
The example compilation and link instruction in the book
C++ -o guess_it guess_it.cc
is an example of how to invoke a compiler and linker from the command line. If you're using Visaul C++ then the IDE will automate the compilation and link process for you when you click the "build" button, so you don't need to worry about doing this from the command line.
On to the error you're seeing in VC++:
The compiler is telling you that it can't find the header file spawn.h, which you've told it that your program needs in the line
#include <spawn.h>
As other on this page have mentioned, spawn.h is a file supplied by the POSIX standard libraries and contains functionality for spawning new processes.
Respectfully, it sounds to me from the way you asked your question ("compile and link instructions") as though you don't really understand what you're doing. Before you delve into multi-threading in C++, I recommend taking a step back and find a beginner's book on C++ using Visual Studio, and start from the beginning. I'm afraid you'll make very little progress unless you take the time to learn the fundamentals, and using the compiler is about as fundamental as it gets!
Good luck!

Resources