Problem passing file into function

I'm writing a program and I have all of it working properly except the last part, the convert_to_wave function. The problem is that it's in a library, not one that I wrote so I'm not sure what I'm doing wrong. I'm passing in the temp file and it should be converted to my wav file but I receive the error "Error reading input file temp.tmp". It generates a .wav file but I can't get it to play the note. I'm not sure exactly where the error is, my guess is with the input to the function. Am I passing a file in correctly? Any suggestions appreciated. If you need more details, I will try and provide them.

fout.open("temp.tmp");
myfile.open("C5.wav");

for ( i = 0; i < NUM_NOTES ; i++ )
{
time_new = file_contents[i][0];
for ( ; time_old < time_new; time_old++ )
{
for ( j = 0; j < buffer; j++ )
{
fout << notes[j];
}

for ( j = 0; j < buffer -1; j++ )
{
notes[j] = .996 * ( notes[j] + notes[j+1] ) / 2.0;
}
notes[buffer] = ( notes[buffer] + notes[0] ) / 2.0;
}
time_old = time_new;
}

convert_to_wave( "temp.tmp", "C5.wav", SAMPLE_RATE, true);

fout.close();
myfile.close();
I don't know what the declaration/definition of convert_to_wave is so I'm just guessing here, but I'm assuming it requires a file object. You can pass the object by changing this:
convert_to_wave( "temp.tmp", "C5.wav", SAMPLE_RATE, true);

To:
convert_to_wave(fout, myFile, SAMPLE_RATE, true);
Unfortunately I don't know the definition of the function either. I'll put the information that I do have at the end of this message. I tried out what you said, but I receive this error when I try. Do you have any idea what that could mean? Thank you!

1>c:\users\7180977\desktop\prog4\prog4.cpp(196): error C2664: 'convert_to_wave' : cannot convert parameter 1 from 'std::ofstream' to 'const char []'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called





convert_to_wave Function
Copy the supplied header and library files to your project directory and add them to your project. In your prog4.cpp file, you will need to include the supplied header file with the following line:
#include "wave.h"
The convert_to_wave function is used in the following way:

CALL: convert_to_wave( inputfilename, outputfilename, SAMPLE_RATE, true);
The prototype is in wave.h

The inputfile is the name of an ASCII text file containing a sequence of numbers. The numbers may be integer or floating point. There must be no characters in the file other than digits, decimal points, and white space. The outputfile is the name of the wave file that you want to create. It should end in .wav and the base filename should be the same as the name of the input file that is given on the command line.
1>c:\users\7180977\desktop\prog4\prog4.cpp(196): error C2664: 'convert_to_wave' : cannot convert parameter 1 from 'std::ofstream' to 'const char []'

That means the function is expecting parameter 1, the first filename, to be a constant character string, which is what you had previously.

CALL: convert_to_wave( inputfilename, outputfilename, SAMPLE_RATE, true);


I'm assuming you're getting an error because the files are still both opened. Try closing them before calling the function.
fout.open("temp.tmp");
for ( i = 0; i < NUM_NOTES ; i++ )
{
....
}
fout.close();

convert_to_wave( "temp.tmp", "C5.wav", SAMPLE_RATE, true);


I've tried it like this too. And still have the same problem. It compiles but the input file just doesn't seem to work. I've tried changing it to a .txt file but that didn't help. I'll add the compiler notes too. I don't know if they help but I guess it can't hurt.

1>------ Build started: Project: Prog4, Configuration: Debug Win32 ------
1>Build started 12/7/2012 8:56:17 PM.
1>InitializeBuildStatus:
1> Touching "Debug\Prog4.unsuccessfulbuild".
1>ClCompile:
1> prog4.cpp
1>c:\users\7180977\desktop\prog4\prog4.cpp(166): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\7180977\desktop\prog4\prog4.cpp(177): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>Manifest:
1> All outputs are up-to-date.
1>LinkEmbedManifest:
1> All outputs are up-to-date.
1> Prog4.vcxproj -> C:\Users\7180977\Desktop\prog4\Debug\Prog4.exe
1>FinalizeBuildStatus:
1> Deleting file "Debug\Prog4.unsuccessfulbuild".
1> Touching "Debug\Prog4.lastbuildstate".
1>
1>Build succeeded.
1>
1>Time Elapsed 00:00:02.40
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
Try deleting the output file, changing the filename to save the output to, maybe C6.wav, delete the input file, and change that name as well. It's possible it doesn't have rights to access the files.

If there is actually important information in the files, don't delete them, just change them.

The warnings you're getting are for converting doubles to ints. I'm not sure of the exact lines you're having problems at, but it indicates there is an issue with lines 166 and 177. Check for an int variable being assigned a decimal variable or value. That could be your actual issue, but only you changing it will you know.
I actually want the doubles to be converted to ints because that is the number of columns in the array. I will try what you said though.
If it's not an issue, I'd like to look over your code quickly to possibly help further debug.

I wouldn't get your hopes too high, however. I've never had luck with any audio functions. Most of them are designed from years ago and may no longer work on current machines, however they should.
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>

#include "wave.h"

using namespace std;


void sound_note ( double file_contents[NUM_NOTES][3], float notes[NUM_NOTES][BASE_SIZE], int buffer, int &note, int &time_old, int& time_new, ofstream &fout);

int main ( int argc, char *argv[] )
{
float notes[NUM_NOTES][BASE_SIZE] = {0}; //contains ring buffer fro each note
double file_contents[NUM_NOTES][3] = {0}; //contains time, note number and volume from file
ifstream fin; //fin is song to be played
ofstream fout, myfile; //fout is temp file, myfile is wav file
int i, j, time_old, time_new, buffer; //time old is current time, time new is time of next note
double tempo;

//open file + check it is open
fin.open (argv[1]);
if ( !fin )
{
cout << "Error. Please right click on prog4 and view properties. In the Configuration Properties menu, select debugging. Enter " ;
cout << "name of file and desired tempo into command arguments line." << endl;
exit(1);
}

//make sure there are 2 or 3 arguments
if (argc != 2 && argc!= 3 )
{
cout << "Error. Please right click on prog4 and view properties. In the Configuration Properties menu, select debugging. Enter " ;
cout << "name of file and desired tempo into command arguments line." << endl;
exit(1);
}


if (argc == 3)
{
//convert tempo from char to double
tempo = atof( argv[2] );

//check it is within the valid range
if (tempo < 0.25 || tempo > 4.0)
{
cout << "Error. Please enter a floating point tempo between 0.25 and 4.0" << endl;
exit(2);
}

}

//set tempo = 1.0 if none given
if (argc == 2)
tempo = 1.0;

//set time = 0
time_old = 0;

//get notes from file
for( i = 0; i < NUM_NOTES && !fin.eof( ); i++ )
{
fin >> file_contents[i][0];
fin >> file_contents[i][1];
fin >> file_contents[i][2];
}

//adjust times for tempo
for( i = 0; i < NUM_NOTES; i++ )
{
notes[i][0] *= ( 1.0 / tempo );
}

fin.close();

//find size of buffer
buffer = SAMPLE_RATE / ( pow ( 2, ( ( 60 - 69 ) / 12.0 ) ) * 440 );

//create ring buffers for notes
for ( j = 0; j < NUM_NOTES; j++ )
{
for ( i = 0; i < buffer; i++ )
{
notes[j][i] = ( ( rand () / ( 32767.0 ) ) -.5 );
}
}

fout.open("temp.tmp");

//set time=0

time_old = 0;
//play each note
for ( i = 0; i < NUM_NOTES; i++ )
{
time_new = file_contents[i][0];
sound_note ( file_contents, notes, buffer, i, time_old, time_new, fout );
time_old = time_new;
}
fout.close();

convert_to_wave( "temp.tmp", "scale.wav", SAMPLE_RATE, true);

return 0;
}

void sound_note ( double file_contents[NUM_NOTES][3], float notes[NUM_NOTES][BASE_SIZE], int buffer, int &note, int &time_old, int& time_new, ofstream &fout)
{
int j;

for ( ; time_old < time_new; time_old++ )
{
for ( j = 0; j < buffer; j++ )
{
fout << notes[note][j];
}

for ( j = 0; j < buffer -1; j++ )
{
notes[note][j] = .996 * ( notes[note][j] + notes[note][j+1] ) / 2.0;
}
notes[note][buffer] = ( notes[note][buffer] + notes[note][0] ) / 2.0;
}
fout.close();
}
Last edited on
Alright, this is probably going to be my last attempt to correct this. I can't predict how that function is going to work, but let's attempt something else. In your call, change "temp2.tmp" to argv[1]. It might work, it might not, but it's worth a shot.

Also, is C5.wav even created? What does your output display? Are you getting any indication that something is failing? If you know how to, it might be worth trying to debug the function to see what's going on with it. If this is a school project, I'd definitely consult a teacher.
Thank you for all of your help. I will talk with my professor on Monday. I just wanted to answer the questions you asked in case anybody else sees this. I tried changing "temp2.tmp" to argv[1]. The program would not compile. Before c5.wav was being created but when I tried to open it, media player would not play the file. When I changed the program to accomodate multiple notes, I now have an error when I run the program that says "Error reading input file 'temp.tmp'. No such file or directory" however, in the folder for my program, it does exist. I can open it and see the data. I think there must be a problem with the convert function. I don't know what else it could be. Thank you again for trying to help me!
Topic archived. No new replies allowed.