blob: a8b4a307e1441420f6326e6fd20f504fc990959f [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include <cfloat>
12#include <cstdio>
13#include <cstdlib>
kwiberg85d8bb02016-02-16 20:39:36 -080014#include <memory>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000015#include <vector>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_processing/transient/transient_detector.h"
18#include "modules/audio_processing/transient/file_utils.h"
19#include "system_wrappers/include/file_wrapper.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000020
21using webrtc::FileWrapper;
22using webrtc::TransientDetector;
pbos@webrtc.org788acd12014-12-15 09:41:24 +000023
24// Application to generate a RTP timing file.
25// Opens the PCM file and divides the signal in frames.
26// Creates a send times array, one for each step.
27// Each block that contains a transient, has an infinite send time.
28// The resultant array is written to a DAT file
29// Returns -1 on error or |lost_packets| otherwise.
30int main(int argc, char* argv[]) {
31 if (argc != 5) {
32 printf("\n%s - Application to generate a RTP timing file.\n\n", argv[0]);
33 printf("%s PCMfile DATfile chunkSize sampleRate\n\n", argv[0]);
34 printf("Opens the PCMfile with sampleRate in Hertz.\n");
35 printf("Creates a send times array, one for each chunkSize ");
36 printf("milliseconds step.\n");
37 printf("Each block that contains a transient, has an infinite send time. ");
38 printf("The resultant array is written to a DATfile.\n\n");
39 return 0;
40 }
41
kwiberg85d8bb02016-02-16 20:39:36 -080042 std::unique_ptr<FileWrapper> pcm_file(FileWrapper::Create());
tommia6219cc2016-06-15 10:30:14 -070043 pcm_file->OpenFile(argv[1], true);
44 if (!pcm_file->is_open()) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000045 printf("\nThe %s could not be opened.\n\n", argv[1]);
46 return -1;
47 }
48
kwiberg85d8bb02016-02-16 20:39:36 -080049 std::unique_ptr<FileWrapper> dat_file(FileWrapper::Create());
tommia6219cc2016-06-15 10:30:14 -070050 dat_file->OpenFile(argv[2], false);
51 if (!dat_file->is_open()) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000052 printf("\nThe %s could not be opened.\n\n", argv[2]);
53 return -1;
54 }
55
56 int chunk_size_ms = atoi(argv[3]);
57 if (chunk_size_ms <= 0) {
58 printf("\nThe chunkSize must be a positive integer\n\n");
59 return -1;
60 }
61
62 int sample_rate_hz = atoi(argv[4]);
63 if (sample_rate_hz <= 0) {
64 printf("\nThe sampleRate must be a positive integer\n\n");
65 return -1;
66 }
67
68 TransientDetector detector(sample_rate_hz);
69 int lost_packets = 0;
70 size_t audio_buffer_length = chunk_size_ms * sample_rate_hz / 1000;
kwiberg85d8bb02016-02-16 20:39:36 -080071 std::unique_ptr<float[]> audio_buffer(new float[audio_buffer_length]);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000072 std::vector<float> send_times;
73
74 // Read first buffer from the PCM test file.
75 size_t file_samples_read = ReadInt16FromFileToFloatBuffer(
76 pcm_file.get(),
77 audio_buffer_length,
78 audio_buffer.get());
79 for (int time = 0; file_samples_read > 0; time += chunk_size_ms) {
80 // Pad the rest of the buffer with zeros.
81 for (size_t i = file_samples_read; i < audio_buffer_length; ++i) {
82 audio_buffer[i] = 0.0;
83 }
84 float value =
85 detector.Detect(audio_buffer.get(), audio_buffer_length, NULL, 0);
86 if (value < 0.5f) {
87 value = time;
88 } else {
89 value = FLT_MAX;
90 ++lost_packets;
91 }
92 send_times.push_back(value);
93
94 // Read next buffer from the PCM test file.
95 file_samples_read = ReadInt16FromFileToFloatBuffer(pcm_file.get(),
96 audio_buffer_length,
97 audio_buffer.get());
98 }
99
100 size_t floats_written = WriteFloatBufferToFile(dat_file.get(),
101 send_times.size(),
102 &send_times[0]);
103
104 if (floats_written == 0) {
105 printf("\nThe send times could not be written to DAT file\n\n");
106 return -1;
107 }
108
109 pcm_file->CloseFile();
110 dat_file->CloseFile();
111
112 return lost_packets;
113}