pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1 | /* |
| 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> |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 14 | #include <memory> |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 15 | #include <vector> |
| 16 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "modules/audio_processing/transient/file_utils.h" |
Karl Wiberg | 6a4d411 | 2018-03-23 10:39:34 +0100 | [diff] [blame] | 18 | #include "modules/audio_processing/transient/transient_detector.h" |
| 19 | #include "rtc_base/system/file_wrapper.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 20 | |
| 21 | using webrtc::FileWrapper; |
| 22 | using webrtc::TransientDetector; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 23 | |
| 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. |
| 30 | int 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 | |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 42 | std::unique_ptr<FileWrapper> pcm_file(FileWrapper::Create()); |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 43 | pcm_file->OpenFile(argv[1], true); |
| 44 | if (!pcm_file->is_open()) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 45 | printf("\nThe %s could not be opened.\n\n", argv[1]); |
| 46 | return -1; |
| 47 | } |
| 48 | |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 49 | std::unique_ptr<FileWrapper> dat_file(FileWrapper::Create()); |
tommi | a6219cc | 2016-06-15 10:30:14 -0700 | [diff] [blame] | 50 | dat_file->OpenFile(argv[2], false); |
| 51 | if (!dat_file->is_open()) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 52 | 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; |
kwiberg | 85d8bb0 | 2016-02-16 20:39:36 -0800 | [diff] [blame] | 71 | std::unique_ptr<float[]> audio_buffer(new float[audio_buffer_length]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 72 | std::vector<float> send_times; |
| 73 | |
| 74 | // Read first buffer from the PCM test file. |
| 75 | size_t file_samples_read = ReadInt16FromFileToFloatBuffer( |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 76 | pcm_file.get(), audio_buffer_length, audio_buffer.get()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 77 | for (int time = 0; file_samples_read > 0; time += chunk_size_ms) { |
| 78 | // Pad the rest of the buffer with zeros. |
| 79 | for (size_t i = file_samples_read; i < audio_buffer_length; ++i) { |
| 80 | audio_buffer[i] = 0.0; |
| 81 | } |
| 82 | float value = |
| 83 | detector.Detect(audio_buffer.get(), audio_buffer_length, NULL, 0); |
| 84 | if (value < 0.5f) { |
| 85 | value = time; |
| 86 | } else { |
| 87 | value = FLT_MAX; |
| 88 | ++lost_packets; |
| 89 | } |
| 90 | send_times.push_back(value); |
| 91 | |
| 92 | // Read next buffer from the PCM test file. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 93 | file_samples_read = ReadInt16FromFileToFloatBuffer( |
| 94 | pcm_file.get(), audio_buffer_length, audio_buffer.get()); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 97 | size_t floats_written = |
| 98 | WriteFloatBufferToFile(dat_file.get(), send_times.size(), &send_times[0]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 99 | |
| 100 | if (floats_written == 0) { |
| 101 | printf("\nThe send times could not be written to DAT file\n\n"); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | pcm_file->CloseFile(); |
| 106 | dat_file->CloseFile(); |
| 107 | |
| 108 | return lost_packets; |
| 109 | } |