Skip to content

Commit

Permalink
* cleaned up (and fixed) code for generating duration
Browse files Browse the repository at this point in the history
  • Loading branch information
beschulz committed Jan 6, 2015
1 parent 56da878 commit 2b7819d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 39 deletions.
43 changes: 19 additions & 24 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,26 @@ int main(int argc, char* argv[])
ofs << " \"_generator\":\"wav2json version " << version::version << " on " << version::platform << " (http://goo.gl/af7wg)\"," << std::endl;
}

for(size_t i = 0; i != options.channels.size(); ++i)
// open sound file
SndfileHandle wav(options.input_file_name.c_str());

// Handle error
if ( wav.error() )
{
// open sound file
SndfileHandle wav(options.input_file_name.c_str());
cerr << "Error opening audio file '" << options.input_file_name << "'" << endl;
cerr << "Error was: '" << wav.strError() << "'" << endl;
return 2;
}

// // output sound duration
// if (i == 0) {
// ofs << " \"duration\":" << wav.frames()/(float)wav.samplerate() << "," << std::endl;
// }

// Handle error
if ( wav.error() )
{
cerr << "Error opening audio file '" << options.input_file_name << "'" << endl;
cerr << "Error was: '" << wav.strError() << "'" << endl;
return 2;
}
for(size_t i = 0; i != options.channels.size(); ++i)
{
wav.seek(0, SEEK_SET);

Options::Channel channel = options.channels[i];

ofs << " \"" << channel << "\":";

float sampleDur = compute_waveform(
compute_waveform(
wav,
ofs,
options.samples,
Expand All @@ -93,16 +90,14 @@ int main(int argc, char* argv[])
progress_callback
);

// if (i != options.channels.size()-1) //only write comma, if this is not the last entry
// ofs << "," << std::endl;

ofs << "," << std::endl;
if (i == options.channels.size()-1) {
ofs << std::endl;
ofs << " \"duration\":" << sampleDur;
}
ofs << "," << std::endl;
}

// increase precission for duration
ofs << std::fixed; //explicitly use fixed notation
ofs.precision( std::numeric_limits<float>::digits10 );

ofs << " \"duration\":" << wav.frames() / float(wav.samplerate());
ofs << std::endl << "}" << std::endl;

cerr << endl;
Expand Down
17 changes: 3 additions & 14 deletions src/wav2json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ T compute_sample(const std::vector<T>& block, int i, int n_channels, Options::Ch
/*
compute the waveform of the supplied audio-file and store it into out_image.
*/
float compute_waveform(
void compute_waveform(
const SndfileHandle& wav,
std::ostream& output_stream,
size_t samples,
Expand Down Expand Up @@ -135,12 +135,8 @@ float compute_waveform(
for (size_t x = 0; x < samples; ++x)
{
// read frames
//int pos = (int)(x*wav.frames()/samples);
//const_cast<SndfileHandle&>(wav).seek(pos, SEEK_SET);
sf_count_t n = const_cast<SndfileHandle&>(wav).readf(&block[0], frames_per_pixel) * wav.channels();
assert(n <= (sf_count_t)block.size());
// std::cout << "frames: " << const_cast<SndfileHandle&>(wav).seek(0, SEEK_CUR) << std::endl;


// find min and max
sample_type max(0);
Expand All @@ -163,20 +159,13 @@ float compute_waveform(
if ( x%(progress_divisor) == 0 )
{
if ( progress_callback && !progress_callback( 100*x/samples ) )
return 0;
return;
}
}

// call the progress callback
if ( progress_callback && !progress_callback( 100 ) )
return 0;
return;

output_stream << "]";
std::cout << "frames: " << const_cast<SndfileHandle&>(wav).seek(0, SEEK_CUR) << std::endl;
std::cout << "frames2: " << frames_per_pixel*samples << std::endl;
std::cout << "sample duration: " << const_cast<SndfileHandle&>(wav).seek(0, SEEK_CUR)/wav.samplerate() << std::endl;
std::cout << "sample duration2: " << (frames_per_pixel*samples)/(float)wav.samplerate() << std::endl;
std::cout << "total frames " << wav.frames() << std::endl;
std::cout << "total duration: " << wav.frames()/(float)wav.samplerate()<< std::endl;
return (frames_per_pixel*samples)/(float)wav.samplerate();
}
2 changes: 1 addition & 1 deletion src/wav2json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
typedef bool (*progress_callback_t)(size_t progress);
#endif /* __OBJC__ */

float compute_waveform(
void compute_waveform(
const SndfileHandle& wav,
std::ostream& output_stream,
size_t samples,
Expand Down

0 comments on commit 2b7819d

Please sign in to comment.