1470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com/*
2a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *
4470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  Use of this source code is governed by a BSD-style license
5470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  that can be found in the LICENSE file in the root of the source
6470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  tree. An additional intellectual property rights grant can be found
7470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  in the file PATENTS.  All contributing project authors may
8470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  be found in the AUTHORS file in the root of the source tree.
9470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com */
10470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
113e6db2321ccdc8738c9cecbe9bdab13d4f0f658dkjellander#ifndef WEBRTC_MODULES_AUDIO_CODING_TEST_PCMFILE_H_
123e6db2321ccdc8738c9cecbe9bdab13d4f0f658dkjellander#define WEBRTC_MODULES_AUDIO_CODING_TEST_PCMFILE_H_
13470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
1412dc1a38ca54a000e4fecfbc6d41138b895c9ca5pbos@webrtc.org#include <stdio.h>
1512dc1a38ca54a000e4fecfbc6d41138b895c9ca5pbos@webrtc.org#include <stdlib.h>
1612dc1a38ca54a000e4fecfbc6d41138b895c9ca5pbos@webrtc.org
17ba46804417a93a5b4691e32b1d7b2af6def091f3tina.legrand@webrtc.org#include <string>
18470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
194d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin#include "webrtc/base/optional.h"
20ff761fba8274d93bd73e76c8b8a1f2d0776dd840Henrik Kjellander#include "webrtc/modules/include/module_common_types.h"
216ea3d1cc9e45ca5661b06ec3390282fa50ded349turaj@webrtc.org#include "webrtc/typedefs.h"
22a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org
23554ae1ad4ed3e6d1d9d807f15324d2f875888d23tina.legrand@webrtc.orgnamespace webrtc {
24470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
25a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.orgclass PCMFile {
26a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org public:
27a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  PCMFile();
280946a56023d821e0deca04029bb016ae1f23aa82pbos@webrtc.org  PCMFile(uint32_t timestamp);
29a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  ~PCMFile() {
30a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org    if (pcm_file_ != NULL) {
31a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org      fclose(pcm_file_);
32470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
33a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  }
34ba46804417a93a5b4691e32b1d7b2af6def091f3tina.legrand@webrtc.org
35d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  void Open(const std::string& filename, uint16_t frequency, const char* mode,
36d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org            bool auto_rewind = false);
37a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org
380946a56023d821e0deca04029bb016ae1f23aa82pbos@webrtc.org  int32_t Read10MsData(AudioFrame& audio_frame);
39a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org
40dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting  void Write10MsData(int16_t *playout_buffer, size_t length_smpls);
41a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  void Write10MsData(AudioFrame& audio_frame);
42470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
430946a56023d821e0deca04029bb016ae1f23aa82pbos@webrtc.org  uint16_t PayloadLength10Ms() const;
440946a56023d821e0deca04029bb016ae1f23aa82pbos@webrtc.org  int32_t SamplingFrequency() const;
45a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  void Close();
46a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  bool EndOfFile() const {
47a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org    return end_of_file_;
48a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  }
494d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  // Moves forward the specified number of 10 ms blocks. If a limit has been set
504d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  // with SetNum10MsBlocksToRead, fast-forwarding does not count towards this
514d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  // limit.
524d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  void FastForward(int num_10ms_blocks);
53a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  void Rewind();
54d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  static int16_t ChooseFile(std::string* file_name, int16_t max_len,
550946a56023d821e0deca04029bb016ae1f23aa82pbos@webrtc.org                            uint16_t* frequency_hz);
56a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  bool Rewinded();
57a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  void SaveStereo(bool is_stereo = true);
58a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  void ReadStereo(bool is_stereo = true);
594d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  // If set, the reading will stop after the specified number of blocks have
604d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  // been read. When that has happened, EndOfFile() will return true. Calling
614d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  // Rewind() will reset the counter and start over.
624d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  void SetNum10MsBlocksToRead(int value);
634d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin
64a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org private:
65a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  FILE* pcm_file_;
660946a56023d821e0deca04029bb016ae1f23aa82pbos@webrtc.org  uint16_t samples_10ms_;
670946a56023d821e0deca04029bb016ae1f23aa82pbos@webrtc.org  int32_t frequency_;
68a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  bool end_of_file_;
69a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  bool auto_rewind_;
70a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  bool rewinded_;
710946a56023d821e0deca04029bb016ae1f23aa82pbos@webrtc.org  uint32_t timestamp_;
72a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  bool read_stereo_;
73a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org  bool save_stereo_;
744d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  rtc::Optional<int> num_10ms_blocks_to_read_;
754d68208a2089bfb7c9bf87f06722033a9e82a2a3Henrik Lundin  int blocks_read_ = 0;
76470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com};
77470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
78a6ecd1ebb5ddee3eebc4d066b8ce6ea5942dfa01tina.legrand@webrtc.org}  // namespace webrtc
79554ae1ad4ed3e6d1d9d807f15324d2f875888d23tina.legrand@webrtc.org
803e6db2321ccdc8738c9cecbe9bdab13d4f0f658dkjellander#endif  // WEBRTC_MODULES_AUDIO_CODING_TEST_PCMFILE_H_
81