Skip to content

Commit

Permalink
improve duration precision
Browse files Browse the repository at this point in the history
  • Loading branch information
imskull committed Dec 15, 2013
1 parent b6b168c commit d95cc05
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
22 changes: 14 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ int main(int argc, char* argv[])
// open sound file
SndfileHandle wav(options.input_file_name.c_str());

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

// handle error
// Handle error
if ( wav.error() )
{
cerr << "Error opening audio file '" << options.input_file_name << "'" << endl;
Expand All @@ -82,7 +82,7 @@ int main(int argc, char* argv[])

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

compute_waveform(
float sampleDur = compute_waveform(
wav,
ofs,
options.samples,
Expand All @@ -93,8 +93,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;
// 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 << "}" << std::endl;
Expand Down
19 changes: 15 additions & 4 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.
*/
void compute_waveform(
float compute_waveform(
const SndfileHandle& wav,
std::ostream& output_stream,
size_t samples,
Expand Down Expand Up @@ -134,9 +134,13 @@ void compute_waveform(
*/
for (size_t x = 0; x < samples; ++x)
{
// read frames
// 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 @@ -159,13 +163,20 @@ void compute_waveform(
if ( x%(progress_divisor) == 0 )
{
if ( progress_callback && !progress_callback( 100*x/samples ) )
return;
return 0;
}
}

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

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__ */

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

0 comments on commit d95cc05

Please sign in to comment.