1 | /* Copyright 2017 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 | // See docs in ../ops/audio_ops.cc |
17 | |
18 | #include "tensorflow/core/framework/bounds_check.h" |
19 | #include "tensorflow/core/framework/op_kernel.h" |
20 | #include "tensorflow/core/framework/register_types.h" |
21 | #include "tensorflow/core/framework/tensor.h" |
22 | #include "tensorflow/core/framework/tensor_shape.h" |
23 | #include "tensorflow/core/framework/types.h" |
24 | #include "tensorflow/core/lib/core/status.h" |
25 | #include "tensorflow/core/lib/wav/wav_io.h" |
26 | |
27 | namespace tensorflow { |
28 | |
29 | // Encode a tensor as audio samples into the contents of a WAV format file. |
30 | class EncodeWavOp : public OpKernel { |
31 | public: |
32 | explicit EncodeWavOp(OpKernelConstruction* context) : OpKernel(context) {} |
33 | |
34 | void Compute(OpKernelContext* context) override { |
35 | const Tensor& audio = context->input(0); |
36 | OP_REQUIRES(context, audio.dims() == 2, |
37 | errors::InvalidArgument("audio must be 2-dimensional" , |
38 | audio.shape().DebugString())); |
39 | const Tensor& sample_rate_tensor = context->input(1); |
40 | OP_REQUIRES(context, TensorShapeUtils::IsScalar(sample_rate_tensor.shape()), |
41 | errors::InvalidArgument( |
42 | "Input sample_rate should be a scalar tensor, got " , |
43 | sample_rate_tensor.shape().DebugString(), " instead." )); |
44 | const int32_t sample_rate = sample_rate_tensor.scalar<int32>()(); |
45 | OP_REQUIRES( |
46 | context, |
47 | FastBoundsCheck(audio.NumElements(), std::numeric_limits<int32>::max()), |
48 | errors::InvalidArgument( |
49 | "Cannot encode audio with >= max int32 elements" )); |
50 | |
51 | const int32_t channel_count = static_cast<int32>(audio.dim_size(1)); |
52 | const int32_t sample_count = static_cast<int32>(audio.dim_size(0)); |
53 | |
54 | // Encode audio to wav string. |
55 | Tensor* output = nullptr; |
56 | OP_REQUIRES_OK(context, |
57 | context->allocate_output(0, TensorShape({}), &output)); |
58 | OP_REQUIRES_OK(context, |
59 | wav::EncodeAudioAsS16LEWav( |
60 | audio.flat<float>().data(), sample_rate, channel_count, |
61 | sample_count, &output->scalar<tstring>()())); |
62 | } |
63 | }; |
64 | REGISTER_KERNEL_BUILDER(Name("EncodeWav" ).Device(DEVICE_CPU), EncodeWavOp); |
65 | |
66 | } // namespace tensorflow |
67 | |