1a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org/*
2a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org *  Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org *
4a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org *  Use of this source code is governed by a BSD-style license
5a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org *  that can be found in the LICENSE file in the root of the source
6a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org *  tree. An additional intellectual property rights grant can be found
7a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org *  in the file PATENTS.  All contributing project authors may
8a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org */
10a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
11a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#ifndef WEBRTC_COMMON_AUDIO_WAV_FILE_H_
12a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#define WEBRTC_COMMON_AUDIO_WAV_FILE_H_
13a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
14a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#ifdef __cplusplus
15a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
16a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#include <stdint.h>
17a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#include <cstddef>
18a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#include <string>
19a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
20bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald#include "webrtc/base/constructormagic.h"
21bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald
22a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.orgnamespace webrtc {
23a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
24bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald// Interface to provide access to WAV file parameters.
25bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonaldclass WavFile {
26bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald public:
27bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald  virtual ~WavFile() {}
28bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald
29bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald  virtual int sample_rate() const = 0;
306955870806624479723addfae6dcf5d13968796cPeter Kasting  virtual size_t num_channels() const = 0;
3125702cb1628941427fa55e528f53483f239ae011pkasting  virtual size_t num_samples() const = 0;
32b0ad43baa02f41dba01be4df9606dc65f24c0ec8aluebs
33b0ad43baa02f41dba01be4df9606dc65f24c0ec8aluebs  // Returns a human-readable string containing the audio format.
34b0ad43baa02f41dba01be4df9606dc65f24c0ec8aluebs  std::string FormatAsString() const;
35bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald};
36bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald
37a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org// Simple C++ class for writing 16-bit PCM WAV files. All error handling is
3891d6edef35e7275879c30ce16ecb8b6dc73c6e4ahenrikg// by calls to RTC_CHECK(), making it unsuitable for anything but debug code.
39bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonaldclass WavWriter final : public WavFile {
40a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org public:
41a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  // Open a new WAV file for writing.
426955870806624479723addfae6dcf5d13968796cPeter Kasting  WavWriter(const std::string& filename, int sample_rate, size_t num_channels);
43a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
44a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  // Close the WAV file, after writing its header.
45a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  ~WavWriter();
46a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
47a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  // Write additional samples to the file. Each sample is in the range
48a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  // [-32768,32767], and there must be the previously specified number of
49a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  // interleaved channels.
50a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  void WriteSamples(const float* samples, size_t num_samples);
51a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  void WriteSamples(const int16_t* samples, size_t num_samples);
52a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
53bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald  int sample_rate() const override { return sample_rate_; }
546955870806624479723addfae6dcf5d13968796cPeter Kasting  size_t num_channels() const override { return num_channels_; }
5525702cb1628941427fa55e528f53483f239ae011pkasting  size_t num_samples() const override { return num_samples_; }
56a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
57a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org private:
58a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  void Close();
59a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  const int sample_rate_;
606955870806624479723addfae6dcf5d13968796cPeter Kasting  const size_t num_channels_;
6125702cb1628941427fa55e528f53483f239ae011pkasting  size_t num_samples_;  // Total number of samples written to file.
62a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  FILE* file_handle_;  // Output file, owned by this class
63bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald
643c089d751ede283e21e186885eaf705c3257ccd2henrikg  RTC_DISALLOW_COPY_AND_ASSIGN(WavWriter);
65a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org};
66a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
67a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org// Follows the conventions of WavWriter.
68bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonaldclass WavReader final : public WavFile {
69a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org public:
70a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  // Opens an existing WAV file for reading.
71a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  explicit WavReader(const std::string& filename);
72a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
73a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  // Close the WAV file.
74a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  ~WavReader();
75a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
76a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  // Returns the number of samples read. If this is less than requested,
77a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  // verifies that the end of the file was reached.
78a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  size_t ReadSamples(size_t num_samples, float* samples);
79a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  size_t ReadSamples(size_t num_samples, int16_t* samples);
80a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
81bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald  int sample_rate() const override { return sample_rate_; }
826955870806624479723addfae6dcf5d13968796cPeter Kasting  size_t num_channels() const override { return num_channels_; }
8325702cb1628941427fa55e528f53483f239ae011pkasting  size_t num_samples() const override { return num_samples_; }
84a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
85a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org private:
86a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  void Close();
87a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  int sample_rate_;
886955870806624479723addfae6dcf5d13968796cPeter Kasting  size_t num_channels_;
8925702cb1628941427fa55e528f53483f239ae011pkasting  size_t num_samples_;  // Total number of samples in the file.
9025702cb1628941427fa55e528f53483f239ae011pkasting  size_t num_samples_remaining_;
91a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org  FILE* file_handle_;  // Input file, owned by this class.
92bc2296de9e3680032d6c632750d7551704cf3f06Andrew MacDonald
933c089d751ede283e21e186885eaf705c3257ccd2henrikg  RTC_DISALLOW_COPY_AND_ASSIGN(WavReader);
94a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org};
95a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
96a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org}  // namespace webrtc
97a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
98a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.orgextern "C" {
99a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#endif  // __cplusplus
100a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
101a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org// C wrappers for the WavWriter class.
102a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.orgtypedef struct rtc_WavWriter rtc_WavWriter;
103a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.orgrtc_WavWriter* rtc_WavOpen(const char* filename,
104a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org                           int sample_rate,
1056955870806624479723addfae6dcf5d13968796cPeter Kasting                           size_t num_channels);
106a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.orgvoid rtc_WavClose(rtc_WavWriter* wf);
107a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.orgvoid rtc_WavWriteSamples(rtc_WavWriter* wf,
108a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org                         const float* samples,
109a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org                         size_t num_samples);
110a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.orgint rtc_WavSampleRate(const rtc_WavWriter* wf);
1116955870806624479723addfae6dcf5d13968796cPeter Kastingsize_t rtc_WavNumChannels(const rtc_WavWriter* wf);
11225702cb1628941427fa55e528f53483f239ae011pkastingsize_t rtc_WavNumSamples(const rtc_WavWriter* wf);
113a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
114a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#ifdef __cplusplus
115a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org}  // extern "C"
116a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#endif
117a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org
118a3ed713dad5ccad03e2f5d775081143babd19097andrew@webrtc.org#endif  // WEBRTC_COMMON_AUDIO_WAV_FILE_H_
119