xref: /aosp_15_r20/external/webrtc/modules/audio_coding/test/PCMFile.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2012 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 #ifndef MODULES_AUDIO_CODING_TEST_PCMFILE_H_
12 #define MODULES_AUDIO_CODING_TEST_PCMFILE_H_
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 
17 #include <string>
18 
19 #include "absl/strings/string_view.h"
20 #include "absl/types/optional.h"
21 #include "api/audio/audio_frame.h"
22 
23 namespace webrtc {
24 
25 class PCMFile {
26  public:
27   PCMFile();
28   PCMFile(uint32_t timestamp);
29   ~PCMFile();
30 
31   void Open(absl::string_view filename,
32             uint16_t frequency,
33             absl::string_view mode,
34             bool auto_rewind = false);
35 
36   int32_t Read10MsData(AudioFrame& audio_frame);
37 
38   void Write10MsData(const int16_t* playout_buffer, size_t length_smpls);
39   void Write10MsData(const AudioFrame& audio_frame);
40 
41   uint16_t PayloadLength10Ms() const;
42   int32_t SamplingFrequency() const;
43   void Close();
EndOfFile()44   bool EndOfFile() const { return end_of_file_; }
45   // 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);
49   void Rewind();
50   static int16_t ChooseFile(std::string* file_name,
51                             int16_t max_len,
52                             uint16_t* frequency_hz);
53   bool Rewinded();
54   void SaveStereo(bool is_stereo = true);
55   void ReadStereo(bool is_stereo = true);
56   // 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 
61  private:
62   FILE* pcm_file_;
63   uint16_t samples_10ms_;
64   int32_t frequency_;
65   bool end_of_file_;
66   bool auto_rewind_;
67   bool rewinded_;
68   uint32_t timestamp_;
69   bool read_stereo_;
70   bool save_stereo_;
71   absl::optional<int> num_10ms_blocks_to_read_;
72   int blocks_read_ = 0;
73 };
74 
75 }  // namespace webrtc
76 
77 #endif  // MODULES_AUDIO_CODING_TEST_PCMFILE_H_
78