2014-12-15 09:41:24 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <cfloat>
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdlib>
|
2016-02-16 20:39:36 -08:00
|
|
|
#include <memory>
|
2014-12-15 09:41:24 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
2017-09-15 06:47:31 +02:00
|
|
|
#include "modules/audio_processing/transient/file_utils.h"
|
2018-03-23 10:39:34 +01:00
|
|
|
#include "modules/audio_processing/transient/transient_detector.h"
|
|
|
|
|
#include "rtc_base/system/file_wrapper.h"
|
2014-12-15 09:41:24 +00:00
|
|
|
|
|
|
|
|
using webrtc::FileWrapper;
|
|
|
|
|
using webrtc::TransientDetector;
|
|
|
|
|
|
|
|
|
|
// Application to generate a RTP timing file.
|
|
|
|
|
// Opens the PCM file and divides the signal in frames.
|
|
|
|
|
// Creates a send times array, one for each step.
|
|
|
|
|
// Each block that contains a transient, has an infinite send time.
|
|
|
|
|
// The resultant array is written to a DAT file
|
|
|
|
|
// Returns -1 on error or |lost_packets| otherwise.
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
if (argc != 5) {
|
|
|
|
|
printf("\n%s - Application to generate a RTP timing file.\n\n", argv[0]);
|
|
|
|
|
printf("%s PCMfile DATfile chunkSize sampleRate\n\n", argv[0]);
|
|
|
|
|
printf("Opens the PCMfile with sampleRate in Hertz.\n");
|
|
|
|
|
printf("Creates a send times array, one for each chunkSize ");
|
|
|
|
|
printf("milliseconds step.\n");
|
|
|
|
|
printf("Each block that contains a transient, has an infinite send time. ");
|
|
|
|
|
printf("The resultant array is written to a DATfile.\n\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 20:39:36 -08:00
|
|
|
std::unique_ptr<FileWrapper> pcm_file(FileWrapper::Create());
|
FileWrapper[Impl] modifications and actually remove the "Impl" class.
This is a somewhat involved refactoring of this class. Here's an overview of the changes:
* FileWrapper can now be used as a regular class and instances allocated on the stack.
* The type now has support for move semantics and copy isn't allowed.
* New public ctor with FILE* that can be used instead of OpenFromFileHandle.
* New static Open() method. The intent of this is to allow opening a file and getting back a FileWrapper instance. Using this method instead of Create(), will allow us in the future to make the FILE* member pointer, to be const and simplify threading (get rid of the lock).
* Rename the Open() method to is_open() and make it inline.
* The FileWrapper interface is no longer a pure virtual interface. There's only one implementation so there's no need to go through a vtable for everything.
* Functionality offered by the class, is now reduced. No support for looping (not clear if that was actually useful to users of that flag), no need to implement the 'read_only_' functionality in the class, since file APIs implement that already, no support for *not* managing the file handle (this wasn't used). OpenFromFileHandle always "manages" the file.
* Delete the unused WriteText() method and don't support opening files in text mode. Text mode is only different on Windows and on Windows it translates \n to \r\n, which means that files such as log files, could have a slightly different format on Windows than other platforms. Besides, tools on Windows can handle UNIX line endings.
* Remove FileName(), change Trace code to manage its own path.
* Rename id_ member variable to file_.
* Removed the open_ member variable since the same functionality can be gotten from just checking the file pointer.
* Don't call CloseFile inside of Write. Write shouldn't be changing the state of the class beyond just attempting to write.
* Remove concept of looping from FileWrapper and never close inside of Read()
* Changed stream base classes to inherit from a common base class instead of both defining the Rewind method. Ultimately, Id' like to remove these interfaces and just have FileWrapper.
* Remove read_only param from OpenFromFileHandle
* Renamed size_in_bytes_ to position_, since it gets set to 0 when Rewind() is called (and the size actually does not change).
* Switch out rw lock for CriticalSection. The r/w lock was only used for reading when checking the open_ flag.
BUG=
Review-Url: https://codereview.webrtc.org/2054373002
Cr-Commit-Position: refs/heads/master@{#13155}
2016-06-15 10:30:14 -07:00
|
|
|
pcm_file->OpenFile(argv[1], true);
|
|
|
|
|
if (!pcm_file->is_open()) {
|
2014-12-15 09:41:24 +00:00
|
|
|
printf("\nThe %s could not be opened.\n\n", argv[1]);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-16 20:39:36 -08:00
|
|
|
std::unique_ptr<FileWrapper> dat_file(FileWrapper::Create());
|
FileWrapper[Impl] modifications and actually remove the "Impl" class.
This is a somewhat involved refactoring of this class. Here's an overview of the changes:
* FileWrapper can now be used as a regular class and instances allocated on the stack.
* The type now has support for move semantics and copy isn't allowed.
* New public ctor with FILE* that can be used instead of OpenFromFileHandle.
* New static Open() method. The intent of this is to allow opening a file and getting back a FileWrapper instance. Using this method instead of Create(), will allow us in the future to make the FILE* member pointer, to be const and simplify threading (get rid of the lock).
* Rename the Open() method to is_open() and make it inline.
* The FileWrapper interface is no longer a pure virtual interface. There's only one implementation so there's no need to go through a vtable for everything.
* Functionality offered by the class, is now reduced. No support for looping (not clear if that was actually useful to users of that flag), no need to implement the 'read_only_' functionality in the class, since file APIs implement that already, no support for *not* managing the file handle (this wasn't used). OpenFromFileHandle always "manages" the file.
* Delete the unused WriteText() method and don't support opening files in text mode. Text mode is only different on Windows and on Windows it translates \n to \r\n, which means that files such as log files, could have a slightly different format on Windows than other platforms. Besides, tools on Windows can handle UNIX line endings.
* Remove FileName(), change Trace code to manage its own path.
* Rename id_ member variable to file_.
* Removed the open_ member variable since the same functionality can be gotten from just checking the file pointer.
* Don't call CloseFile inside of Write. Write shouldn't be changing the state of the class beyond just attempting to write.
* Remove concept of looping from FileWrapper and never close inside of Read()
* Changed stream base classes to inherit from a common base class instead of both defining the Rewind method. Ultimately, Id' like to remove these interfaces and just have FileWrapper.
* Remove read_only param from OpenFromFileHandle
* Renamed size_in_bytes_ to position_, since it gets set to 0 when Rewind() is called (and the size actually does not change).
* Switch out rw lock for CriticalSection. The r/w lock was only used for reading when checking the open_ flag.
BUG=
Review-Url: https://codereview.webrtc.org/2054373002
Cr-Commit-Position: refs/heads/master@{#13155}
2016-06-15 10:30:14 -07:00
|
|
|
dat_file->OpenFile(argv[2], false);
|
|
|
|
|
if (!dat_file->is_open()) {
|
2014-12-15 09:41:24 +00:00
|
|
|
printf("\nThe %s could not be opened.\n\n", argv[2]);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int chunk_size_ms = atoi(argv[3]);
|
|
|
|
|
if (chunk_size_ms <= 0) {
|
|
|
|
|
printf("\nThe chunkSize must be a positive integer\n\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sample_rate_hz = atoi(argv[4]);
|
|
|
|
|
if (sample_rate_hz <= 0) {
|
|
|
|
|
printf("\nThe sampleRate must be a positive integer\n\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TransientDetector detector(sample_rate_hz);
|
|
|
|
|
int lost_packets = 0;
|
|
|
|
|
size_t audio_buffer_length = chunk_size_ms * sample_rate_hz / 1000;
|
2016-02-16 20:39:36 -08:00
|
|
|
std::unique_ptr<float[]> audio_buffer(new float[audio_buffer_length]);
|
2014-12-15 09:41:24 +00:00
|
|
|
std::vector<float> send_times;
|
|
|
|
|
|
|
|
|
|
// Read first buffer from the PCM test file.
|
|
|
|
|
size_t file_samples_read = ReadInt16FromFileToFloatBuffer(
|
|
|
|
|
pcm_file.get(),
|
|
|
|
|
audio_buffer_length,
|
|
|
|
|
audio_buffer.get());
|
|
|
|
|
for (int time = 0; file_samples_read > 0; time += chunk_size_ms) {
|
|
|
|
|
// Pad the rest of the buffer with zeros.
|
|
|
|
|
for (size_t i = file_samples_read; i < audio_buffer_length; ++i) {
|
|
|
|
|
audio_buffer[i] = 0.0;
|
|
|
|
|
}
|
|
|
|
|
float value =
|
|
|
|
|
detector.Detect(audio_buffer.get(), audio_buffer_length, NULL, 0);
|
|
|
|
|
if (value < 0.5f) {
|
|
|
|
|
value = time;
|
|
|
|
|
} else {
|
|
|
|
|
value = FLT_MAX;
|
|
|
|
|
++lost_packets;
|
|
|
|
|
}
|
|
|
|
|
send_times.push_back(value);
|
|
|
|
|
|
|
|
|
|
// Read next buffer from the PCM test file.
|
|
|
|
|
file_samples_read = ReadInt16FromFileToFloatBuffer(pcm_file.get(),
|
|
|
|
|
audio_buffer_length,
|
|
|
|
|
audio_buffer.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t floats_written = WriteFloatBufferToFile(dat_file.get(),
|
|
|
|
|
send_times.size(),
|
|
|
|
|
&send_times[0]);
|
|
|
|
|
|
|
|
|
|
if (floats_written == 0) {
|
|
|
|
|
printf("\nThe send times could not be written to DAT file\n\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pcm_file->CloseFile();
|
|
|
|
|
dat_file->CloseFile();
|
|
|
|
|
|
|
|
|
|
return lost_packets;
|
|
|
|
|
}
|