blob: 5320aa63d06268ed141d68b930f37d04a027cbaa [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_CODING_TEST_PCMFILE_H_
12#define MODULES_AUDIO_CODING_TEST_PCMFILE_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000014#include <stdio.h>
15#include <stdlib.h>
16
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000017#include <string>
niklase@google.com470e71d2011-07-07 08:21:25 +000018
Ali Tofigh714e3cb2022-07-20 12:53:07 +020019#include "absl/strings/string_view.h"
Danil Chapovalovb6021232018-06-19 13:26:36 +020020#include "absl/types/optional.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020021#include "api/audio/audio_frame.h"
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000022
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000023namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000024
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000025class PCMFile {
26 public:
27 PCMFile();
pbos@webrtc.org0946a562013-04-09 00:28:06 +000028 PCMFile(uint32_t timestamp);
kwiberg65fc8b92016-08-29 10:05:24 -070029 ~PCMFile();
tina.legrand@webrtc.orgba468042012-08-17 10:38:28 +000030
Ali Tofigh714e3cb2022-07-20 12:53:07 +020031 void Open(absl::string_view filename,
Yves Gerey665174f2018-06-19 15:03:05 +020032 uint16_t frequency,
Ali Tofigh714e3cb2022-07-20 12:53:07 +020033 absl::string_view mode,
tina.legrand@webrtc.orgd5726a12013-05-03 07:34:12 +000034 bool auto_rewind = false);
niklase@google.com470e71d2011-07-07 08:21:25 +000035
pbos@webrtc.org0946a562013-04-09 00:28:06 +000036 int32_t Read10MsData(AudioFrame& audio_frame);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000037
Yves Gerey665174f2018-06-19 15:03:05 +020038 void Write10MsData(const int16_t* playout_buffer, size_t length_smpls);
yujo36b1a5f2017-06-12 12:45:32 -070039 void Write10MsData(const AudioFrame& audio_frame);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000040
pbos@webrtc.org0946a562013-04-09 00:28:06 +000041 uint16_t PayloadLength10Ms() const;
42 int32_t SamplingFrequency() const;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000043 void Close();
Yves Gerey665174f2018-06-19 15:03:05 +020044 bool EndOfFile() const { return end_of_file_; }
Henrik Lundin4d682082015-12-10 16:24:39 +010045 // Moves forward the specified number of 10 ms blocks. If a limit has been set
46 // with SetNum10MsBlocksToRead, fast-forwarding does not count towards this
47 // limit.
48 void FastForward(int num_10ms_blocks);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000049 void Rewind();
Yves Gerey665174f2018-06-19 15:03:05 +020050 static int16_t ChooseFile(std::string* file_name,
51 int16_t max_len,
pbos@webrtc.org0946a562013-04-09 00:28:06 +000052 uint16_t* frequency_hz);
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000053 bool Rewinded();
54 void SaveStereo(bool is_stereo = true);
55 void ReadStereo(bool is_stereo = true);
Henrik Lundin4d682082015-12-10 16:24:39 +010056 // If set, the reading will stop after the specified number of blocks have
57 // been read. When that has happened, EndOfFile() will return true. Calling
58 // Rewind() will reset the counter and start over.
59 void SetNum10MsBlocksToRead(int value);
60
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000061 private:
62 FILE* pcm_file_;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000063 uint16_t samples_10ms_;
64 int32_t frequency_;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000065 bool end_of_file_;
66 bool auto_rewind_;
67 bool rewinded_;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000068 uint32_t timestamp_;
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000069 bool read_stereo_;
70 bool save_stereo_;
Danil Chapovalovb6021232018-06-19 13:26:36 +020071 absl::optional<int> num_10ms_blocks_to_read_;
Henrik Lundin4d682082015-12-10 16:24:39 +010072 int blocks_read_ = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000073};
74
tina.legrand@webrtc.orga6ecd1e2012-04-26 07:54:30 +000075} // namespace webrtc
tina.legrand@webrtc.org554ae1a2011-12-16 10:09:04 +000076
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020077#endif // MODULES_AUDIO_CODING_TEST_PCMFILE_H_