1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 // Functions to write audio in WAV format.
17
18 #ifndef TENSORFLOW_LIB_WAV_WAV_IO_H_
19 #define TENSORFLOW_LIB_WAV_WAV_IO_H_
20
21 #include <string>
22 #include <vector>
23
24 #include "tensorflow/core/lib/core/coding.h"
25 #include "tensorflow/core/lib/core/errors.h"
26 #include "tensorflow/core/lib/core/status.h"
27 #include "tensorflow/core/platform/types.h"
28
29 namespace tensorflow {
30 namespace wav {
31
32 // Encode the provided interleaved buffer of audio as a signed 16-bit PCM
33 // little-endian WAV file.
34 //
35 // Example usage for 4 frames of an 8kHz stereo signal:
36 // First channel is -1, 1, -1, 1.
37 // Second channel is 0, 0, 0, 0.
38 //
39 // float audio_buffer[] = { -1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f};
40 // string wav_string;
41 // if (EncodeAudioAsS16LEWav(audio_buffer, 8000, 2, 4, &wav_string).ok()) {
42 // // Use wav_string.
43 // }
44 Status EncodeAudioAsS16LEWav(const float* audio, size_t sample_rate,
45 size_t num_channels, size_t num_frames,
46 string* wav_string);
47
48 // Decodes the little-endian signed 16-bit PCM WAV file data (aka LIN16
49 // encoding) into a float Tensor. The channels are encoded as the lowest
50 // dimension of the tensor, with the number of frames as the second. This means
51 // that a four frame stereo signal will have the shape [4, 2]. The sample rate
52 // is read from the file header, and an error is returned if the format is not
53 // supported.
54 // The results are output as floats within the range -1 to 1,
55 Status DecodeLin16WaveAsFloatVector(const string& wav_string,
56 std::vector<float>* float_values,
57 uint32* sample_count, uint16* channel_count,
58 uint32* sample_rate);
59
60 // Everything below here is only exposed publicly for testing purposes.
61
62 // Handles moving the data index forward, validating the arguments, and avoiding
63 // overflow or underflow.
64 Status IncrementOffset(int old_offset, size_t increment, size_t max_size,
65 int* new_offset);
66
67 // This function is only exposed in the header for testing purposes, as a
68 // template that needs to be instantiated. Reads a typed numeric value from a
69 // stream of data.
70 template <class T>
ReadValue(const string & data,T * value,int * offset)71 Status ReadValue(const string& data, T* value, int* offset) {
72 int new_offset;
73 TF_RETURN_IF_ERROR(
74 IncrementOffset(*offset, sizeof(T), data.size(), &new_offset));
75 if (port::kLittleEndian) {
76 memcpy(value, data.data() + *offset, sizeof(T));
77 } else {
78 *value = 0;
79 const uint8* data_buf =
80 reinterpret_cast<const uint8*>(data.data() + *offset);
81 int shift = 0;
82 for (int i = 0; i < sizeof(T); ++i, shift += 8) {
83 *value = *value | (data_buf[i] << shift);
84 }
85 }
86 *offset = new_offset;
87 return Status::OK();
88 }
89
90 } // namespace wav
91 } // namespace tensorflow
92
93 #endif // TENSORFLOW_LIB_WAV_WAV_IO_H_
94