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 #define EIGEN_USE_THREADS
17
18 #include <functional>
19 #include <memory>
20 #include <vector>
21
22 #include "tensorflow/cc/client/client_session.h"
23 #include "tensorflow/cc/ops/audio_ops.h"
24 #include "tensorflow/cc/ops/const_op.h"
25 #include "tensorflow/cc/ops/math_ops.h"
26 #include "tensorflow/core/framework/tensor_testutil.h"
27 #include "tensorflow/core/framework/types.h"
28 #include "tensorflow/core/framework/types.pb.h"
29 #include "tensorflow/core/kernels/ops_util.h"
30 #include "tensorflow/core/lib/core/status_test_util.h"
31 #include "tensorflow/core/platform/test.h"
32
33 namespace tensorflow {
34 namespace ops {
35 namespace {
36
TEST(EncodeWavOpTest,EncodeWavTest)37 TEST(EncodeWavOpTest, EncodeWavTest) {
38 Scope root = Scope::DisabledShapeInferenceScope();
39
40 Tensor audio_tensor(DT_FLOAT, {4, 2});
41 test::FillValues<float>(
42 &audio_tensor, {0.0f, 0.5f, 1.0f, -1.0f, 0.25f, 0.75f, 1.25f, -0.5f});
43 Output audio_op =
44 Const(root.WithOpName("audio_op"), Input::Initializer(audio_tensor));
45
46 Output sample_rate_op = Const(root.WithOpName("sample_rate_op"), 44100);
47
48 EncodeWav encode_wav_op =
49 EncodeWav(root.WithOpName("encode_wav_op"), audio_op, sample_rate_op);
50
51 DecodeWav decode_wav_op =
52 DecodeWav(root.WithOpName("decode_wav_op"), encode_wav_op);
53
54 TF_ASSERT_OK(root.status());
55
56 ClientSession session(root);
57 std::vector<Tensor> outputs;
58
59 TF_EXPECT_OK(session.Run(ClientSession::FeedType(),
60 {decode_wav_op.audio, decode_wav_op.sample_rate},
61 &outputs));
62
63 const Tensor& audio = outputs[0];
64 const int sample_rate = outputs[1].flat<int32>()(0);
65
66 EXPECT_EQ(2, audio.dims());
67 EXPECT_EQ(2, audio.dim_size(1));
68 EXPECT_EQ(4, audio.dim_size(0));
69 EXPECT_NEAR(0.0f, audio.flat<float>()(0), 1e-4f);
70 EXPECT_NEAR(0.5f, audio.flat<float>()(1), 1e-4f);
71 EXPECT_NEAR(1.0f, audio.flat<float>()(2), 1e-4f);
72 EXPECT_NEAR(-1.0f, audio.flat<float>()(3), 1e-4f);
73 EXPECT_NEAR(0.25f, audio.flat<float>()(4), 1e-4f);
74 EXPECT_NEAR(0.75f, audio.flat<float>()(5), 1e-4f);
75 EXPECT_NEAR(1.0f, audio.flat<float>()(6), 1e-4f);
76 EXPECT_NEAR(-0.5f, audio.flat<float>()(7), 1e-4f);
77 EXPECT_EQ(44100, sample_rate);
78 }
79
80 } // namespace
81 } // namespace ops
82 } // namespace tensorflow
83