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!
Related
I'm trying to insert a song that plays on a loop in my c++ Allegro 5 game. It keeps saying that it can't load the audio.
I have:
tried to use .wav and .ogg files, both did not work.
put the audio file in the correct directory.
created a function to detect the error.
initialized al_init_acodec_addon() and al_install_audio()
ALLEGRO_SAMPLE* song = al_load_sample("liar.ogg");
void game_begin()
{
if (!song)
{
printf( "Audio clip sample not loaded!\n" );
show_err_msg(-6);
}
//Loop the song until the display closes
al_play_sample(song, 1,0,1,ALLEGRO_PLAYMODE_LOOP, NULL);
Basically the console almost always prints out the error message no matter what I do.
Is this a known Allegro 5 problem? I still can't think of a way to fix this...
On a side note, I have tested loading & playing audio in another project file and it worked. Is my file cursed? :(
:)
Try to put it in THIS ORDER before loading samples:
if(!al_init())
return -1;
if(!al_install_audio())
return -2;
if!(!al_init_acodec_addon()) // after installing audio
return -3;
if(!al_reserve_samples(1))
return -4;
And what do you mean by correct directory?
I have been scratching my head around this problem for a while, and I couldn't find any answer through surfing the web either.
The problem is that I call system("csvtojson someFile.csv 1> someOtherFile.json") inside my program to produce a JSON file. After this line I want to open, read, and process the JSON file. Although, I can see that the file is created, but fopen() returns NULL.
I read that system() is synchronized so I think the rest of my program will not get executed until the system call is finished, and so the file will be created.
I suspect the problem is somehow related to redirecting the output stream using "1>"; not sure, though.
Any help or hint will be much appreciated.
Thanks! :)
P.S. I don't want to use a library to convert csv to JSON, and I can't perform the conversion outside the program because there are tons of very large csv files and the only way for me is to convert each to a JSON file inside the program, run my algorithm, and move to the next csv file ( converting it to JSON and saving it in the very same JSON file). So in total I have only one JSON file, being like a buffer for my csv files. Having said that, if anyone has a better design approach that can be implemented quickly, that would be also great.
UPDATE : Actual code that exhibits the problem, copied from the OP's answer:
int main(){
system("csvtojson Test_Trace.csv 1> ~/Traces/Test_Trace.json");
FILE* traceFile = fopen("~/Traces/Test_Trace.json", "r");
if(traceFile == NULL)
perror("Error in Openning the trace file");
else
cout << "Successfull openning of the trace file!" << endl;
return 0;
}
Thank you guys for your answers. I had to be more detailed in my question as the problem seemed to be somewhere that wasn't clear from my question.
I figured out what was the problem, and would like to share it here (not a super interesting finding, but worth mentioning).
I wrote a simple program to find the problem:
int main(){
system("csvtojson Test_Trace.csv 1> ~/Traces/Test_Trace.json");
FILE* traceFile = fopen("~/Traces/Test_Trace.json", "r");
if(traceFile == NULL)
perror("Error in Openning the trace file");
else
cout << "Successfull openning of the trace file!" << endl;
return 0;
}
If you run this program you will get the error message No such file or directory, but if you replace the address string with the absolute location, i.e., /home/USER_ID/Traces/Test_Trace.json, in both system(...) and fopen(...) calls, your code will work fine. Interestingly, myself suspected that this could be the problem and I changed just the one for system(...) but still it wasn't working (though the file was being created in the location that was passed to fopen(...)).
EDIT: Thanks to #Peter's comment, this problem was because system() call takes care of ~, but fopen() does not and need an absolute path. So there is really no need to have both functions been given the absolute path.
Anyhow,
Thanks Again. :)
Perhaps the reason for this is because the system command hasn't finished executing by the time your program continues to the next instructions where it tries to read from the file that hasn't been created yet.
Although, this isn't the best way, putting in a short pause might make the difference, or at least let you know if that is the issue.
TL;DR I am trying to take a stream of data and make it write to a .csv file. Everything is worked out except the writing part, which I think is simply due to me not referencing the .csv file correctly. But I'm a newbie to this stuff, and can't figure out how to correctly reference it, so I need help.
Hello, and a big thank you in advance to anyone that can help me out with this! Some advance info, my IDE is Xcode, using C++, and I'm using the Myo armband from Thalmic Labs as a device to collect data. There is a program (link for those interested enough to look at it) that is supposed to stream the EMG, accelerometer, gyroscope, and orientation values into a .csv file. I am so close to getting the app to work, but my lack of programming experience has finally caught up to me, and I am stuck on something rather simple. I know that the app can stream the data, as I have been able to make it print the EMG values in the debugging area. I can also get the app to open a .csv file, using this code:
const char *path= "/Users/username/folder/filename";
std::ofstream file(path);
std::string data("data to write to file");
file << data;
But no data ends up being streamed/printed into that file after I end the program. The only thing that I can think might be causing this is that the print function is not correctly referencing this file pathway. I would assume that to be a straightforward thing, but like I said, I am inexperienced, and do not know exactly how to address this. I am not sure what other information is necessary, so I'll just provide everything that I imagine might be helpful.
This is the function structure that is supposed to open the files: (Note: The app is intended to open the file in the same directory as itself)
void openFiles() {
time_t timestamp = std::time(0);
// Open file for EMG log
if (emgFile.is_open())
{
emgFile.close();
}
std::ostringstream emgFileString;
emgFileString << "emg-" << timestamp << ".csv";
emgFile.open(emgFileString.str(), std::ios::out);
emgFile << "timestamp,emg1,emg2,emg3,emg4,emg5,emg6,emg7,emg8" << std::endl;
This is the helper to print accelerometer and gyroscope data (There doesn't appear to be anything like this to print EMG data, but I know it does, so... Watevs):
void printVector(std::ofstream &path, uint64_t timestamp, const myo::Vector3< float > &vector)
{
path << timestamp
<< ',' << vector.x()
<< ',' << vector.y()
<< ',' << vector.z()
<< std::endl;
}
And this is the function structure that utilizes the helper:
void onAccelerometerData(myo::Myo *myo, uint64_t timestamp, const myo::Vector3< float > &accel)
{
printVector(accelerometerFile, timestamp, accel);
}
I spoke with a staff member at Thalmic Labs (the guy who made the app actually) and he said it sounded like, unless the app was just totally broken, I was potentially just having problems with the permissions on my computer. There are multiple users on this computer, so that may very well be the case, though I certainly hope not, and I'd still like to try and figure it out one more time before throwing in the towel. Again, thanks to anyone who can be of assistance! :)
My imagination is failing me. Have you tried writing to or reading from ostringstream or istringstream objects? That might be informative. Here's a line that's correct:
std::ofstream outputFile( strOutputFilename.c_str(), std::ios::app );
Note that C++ doesn't have any native support for streaming .csv code, though, you may have to do those conversions yourself. :( Things may work better if you replace the "/"'s by (doubled) "//" 's ...
Logger for my program. I saw in another program that it’s somehow possible to open and read a file with text editor while the program is still using it. Seems it just opens a copy for me and continue logging in the background. This kind of log system I need too. But if I use fopen() I only can open and read the file with my text editor if the Programm already closed it with fclose(); This way would work but I think its a very bad solution and also very slow... to open and close the file on every log :S
Someone knows how the needed log system is working?
P.S. I'm working in VisualStudio 2013 on Windows 8.1
Sry for my bad English :S
There are 2 different problems.
First is writing of logs. In a Windows system, the buffering will cause the data to be actually written to disk :
if you close the file
when you have a fair quantity of new data (unsure between several ko and several Mo)
if you explicitely flush
Unless if you have a high throughput, I would advise to at least flush (if not close) after each write to avoid loosing logs if program crashes. And it also allows you to read the log file in real time.
Second is reading. Vim for example is known to be able to monitor a file that can be modified by an external process. It will open a popup saying that file has been modified and offer to reload it. I do not know what notepad does in same conditions. But :
it does not have sense unless first problem has gone
it is not very efficient since you will reload whole file each time
IMHO, you'd better write a custom reader that mimics Linux tail -f :
read (and display) until end of file
repeteadly read (with a short sleep after an unsuccessful read) to process newly added data
It all depends on the text editor you are using. Some will notice edit to the file and ask you if you want to reload a fresh version.
If you work on linux, and you'd like to have an idea of what's happening in real time you could do someting like
tail -f <path-to-file>
or if the file doesnt yet exist
watch -n 0,2 "cat <path-to-file> | tail"
which will display the content of the file and refresh it every 0.2 sec
Thx for your fast answers :)
Crazy.. i was working so long with fopen() and found no solution.. also the fflush(pFile) didnt help (I wasnt able to open file.. always error that its already in use by another program). I never tryed the fstream. Seems fstream solved my problem now. I can open my file with msnotepad.exe while the program is still writing to the file :) Here a small test-code:
#include <fstream> #include <iostream> using namespace std;
int main(){
ofstream FILE;
FILE.open("E:\\Log.txt");
for (size_t i = 0; i < 50; i++)
{
FILE << "Hello " << i << endl;
cout << "log" << endl;
_sleep(500);
}
FILE.close();
cout << "finish" << endl;
return 0;}
I am trying to record a video using openCV's class VideoWriter.
Using the following function,
void addFrameToVideo(Mat& frame)
{
if(_vid.open("test.avi", CV_FOURCC('M','J','P','G'), 30, frame.size(), 1))
cout << "here" << endl;
_vid.write(frame);
}
_vid is a variable of type VideoWriter and I can imshow frame with no problems.
The if condition never verifies, since the program never outputs "here".
However, this code actually creates a "test.avi" file in the directory, but it remains empty.
I've tried several different compression methods with no success.
Any thoughts?
It is possible that the codec could not be found in your system. You should try using different codecs. Alternatively, you can pass the value -1 instead of CV_FOURCC() and a window will pop up at runtime that contains all the codec installed on your system and ask you to select the one to use (this only works on Windows).