1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "common/common.h"
18 #include "minddata/dataset/core/tensor.h"
19 #include "minddata/dataset/include/dataset/datasets.h"
20 #include "minddata/dataset/include/dataset/audio.h"
21
22 using namespace mindspore::dataset;
23 using mindspore::LogStream;
24 using mindspore::ExceptionType::NoExceptionType;
25 using mindspore::MsLogLevel::INFO;
26
27 class MindDataTestPipeline : public UT::DatasetOpTesting {
28 protected:
29 };
30
TEST_F(MindDataTestPipeline,TestTimeMaskingPipeline)31 TEST_F(MindDataTestPipeline, TestTimeMaskingPipeline) {
32 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTimeMaskingPipeline.";
33 // Original waveform
34 std::shared_ptr<SchemaObj> schema = Schema();
35 ASSERT_OK(schema->add_column("inputData", mindspore::DataType::kNumberTypeFloat32, {2, 200}));
36 std::shared_ptr<Dataset> ds = RandomData(50, schema);
37 EXPECT_NE(ds, nullptr);
38
39 ds = ds->SetNumWorkers(4);
40 EXPECT_NE(ds, nullptr);
41
42 auto timemasking = audio::TimeMasking(true, 6);
43
44 ds = ds->Map({timemasking});
45 EXPECT_NE(ds, nullptr);
46
47 // mask waveform
48 std::shared_ptr<Iterator> iter = ds->CreateIterator();
49 EXPECT_NE(ds, nullptr);
50
51 std::unordered_map<std::string, mindspore::MSTensor> row;
52 ASSERT_OK(iter->GetNextRow(&row));
53
54 std::vector<int64_t> expected = {2, 200};
55
56 int i = 0;
57 while (row.size() != 0) {
58 auto col = row["inputData"];
59 ASSERT_EQ(col.Shape(), expected);
60 ASSERT_EQ(col.Shape().size(), 2);
61 ASSERT_EQ(col.DataType(), mindspore::DataType::kNumberTypeFloat32);
62 ASSERT_OK(iter->GetNextRow(&row));
63 i++;
64 }
65 EXPECT_EQ(i, 50);
66
67 iter->Stop();
68 }
69
TEST_F(MindDataTestPipeline,TestTimeMaskingWrongArgs)70 TEST_F(MindDataTestPipeline, TestTimeMaskingWrongArgs) {
71 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTimeMaskingWrongArgs.";
72 // Original waveform
73 std::shared_ptr<SchemaObj> schema = Schema();
74 ASSERT_OK(schema->add_column("inputData", mindspore::DataType::kNumberTypeFloat32, {2, 20}));
75 std::shared_ptr<Dataset> ds = RandomData(50, schema);
76 EXPECT_NE(ds, nullptr);
77
78 ds = ds->SetNumWorkers(4);
79 EXPECT_NE(ds, nullptr);
80
81 auto timemasking = audio::TimeMasking(true, -100);
82
83 ds = ds->Map({timemasking});
84 EXPECT_NE(ds, nullptr);
85
86 std::shared_ptr<Iterator> iter = ds->CreateIterator();
87 // Expect failure
88 EXPECT_EQ(iter, nullptr);
89 }
90
TEST_F(MindDataTestPipeline,TestTimeStretchPipeline)91 TEST_F(MindDataTestPipeline, TestTimeStretchPipeline) {
92 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTimeStretchPipeline.";
93 // op param
94 int freq = 1025;
95 int hop_length = 512;
96 float rate = 1.2;
97 // Original waveform
98 std::shared_ptr<SchemaObj> schema = Schema();
99 ASSERT_OK(schema->add_column("inputData", mindspore::DataType::kNumberTypeFloat32, {2, freq, 400, 2}));
100 std::shared_ptr<Dataset> ds = RandomData(50, schema);
101 EXPECT_NE(ds, nullptr);
102
103 ds = ds->SetNumWorkers(4);
104 EXPECT_NE(ds, nullptr);
105
106 auto TimeStretchOp = audio::TimeStretch(hop_length, freq, rate);
107
108 ds = ds->Map({TimeStretchOp});
109 EXPECT_NE(ds, nullptr);
110
111 // apply timestretch
112 std::shared_ptr<Iterator> iter = ds->CreateIterator();
113 EXPECT_NE(ds, nullptr);
114
115 std::unordered_map<std::string, mindspore::MSTensor> row;
116 ASSERT_OK(iter->GetNextRow(&row));
117
118 std::vector<int64_t> expected = {2, freq, static_cast<int64_t>(std::ceil(400 / rate)), 2};
119
120 int i = 0;
121 while (row.size() != 0) {
122 auto col = row["inputData"];
123 ASSERT_EQ(col.Shape(), expected);
124 ASSERT_EQ(col.DataType(), mindspore::DataType::kNumberTypeFloat32);
125 ASSERT_OK(iter->GetNextRow(&row));
126 i++;
127 }
128 EXPECT_EQ(i, 50);
129
130 iter->Stop();
131 }
132
TEST_F(MindDataTestPipeline,TestTimeStretchPipelineWrongArgs)133 TEST_F(MindDataTestPipeline, TestTimeStretchPipelineWrongArgs) {
134 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestTimeStretchPipelineWrongArgs.";
135 // op param
136 int freq = 1025;
137 int hop_length = 512;
138 float rate = -2;
139 // Original waveform
140 std::shared_ptr<SchemaObj> schema = Schema();
141 ASSERT_OK(schema->add_column("inputData", mindspore::DataType::kNumberTypeFloat32, {2, freq, 400, 2}));
142 std::shared_ptr<Dataset> ds = RandomData(50, schema);
143 EXPECT_NE(ds, nullptr);
144
145 ds = ds->SetNumWorkers(4);
146 EXPECT_NE(ds, nullptr);
147
148 auto TimeStretchOp = audio::TimeStretch(hop_length, freq, rate);
149
150 ds = ds->Map({TimeStretchOp});
151 EXPECT_NE(ds, nullptr);
152
153 // apply timestretch
154 std::shared_ptr<Iterator> iter = ds->CreateIterator();
155 // Expect failure
156 EXPECT_EQ(iter, nullptr);
157 }
158
TEST_F(MindDataTestPipeline,TestVolPipeline)159 TEST_F(MindDataTestPipeline, TestVolPipeline) {
160 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVolPipeline.";
161 // Original waveform
162 std::shared_ptr<SchemaObj> schema = Schema();
163 ASSERT_OK(schema->add_column("inputData", mindspore::DataType::kNumberTypeFloat32, {2, 200}));
164 std::shared_ptr<Dataset> ds = RandomData(50, schema);
165 EXPECT_NE(ds, nullptr);
166
167 ds = ds->SetNumWorkers(4);
168 EXPECT_NE(ds, nullptr);
169
170 auto vol = audio::Vol(0.3);
171
172 ds = ds->Map({vol});
173 EXPECT_NE(ds, nullptr);
174
175 std::shared_ptr<Iterator> iter = ds->CreateIterator();
176 EXPECT_NE(ds, nullptr);
177
178 std::unordered_map<std::string, mindspore::MSTensor> row;
179 ASSERT_OK(iter->GetNextRow(&row));
180
181 std::vector<int64_t> expected = {2, 200};
182
183 int i = 0;
184 while (row.size() != 0) {
185 auto col = row["inputData"];
186 ASSERT_EQ(col.Shape(), expected);
187 ASSERT_EQ(col.Shape().size(), 2);
188 ASSERT_EQ(col.DataType(), mindspore::DataType::kNumberTypeFloat32);
189 ASSERT_OK(iter->GetNextRow(&row));
190 i++;
191 }
192 EXPECT_EQ(i, 50);
193
194 iter->Stop();
195 }
196
TEST_F(MindDataTestPipeline,TestVolWrongArgs)197 TEST_F(MindDataTestPipeline, TestVolWrongArgs) {
198 MS_LOG(INFO) << "Doing MindDataTestPipeline-TestVolWrongArgs.";
199 // Original waveform
200 std::shared_ptr<SchemaObj> schema = Schema();
201 ASSERT_OK(schema->add_column("inputData", mindspore::DataType::kNumberTypeFloat32, {2, 200}));
202 std::shared_ptr<Dataset> ds = RandomData(50, schema);
203 EXPECT_NE(ds, nullptr);
204
205 ds = ds->SetNumWorkers(4);
206 EXPECT_NE(ds, nullptr);
207
208 auto vol_op = audio::Vol(-1.5, GainType::kPower);
209
210 ds = ds->Map({vol_op});
211 EXPECT_NE(ds, nullptr);
212
213 std::shared_ptr<Iterator> iter = ds->CreateIterator();
214 // Expect failure
215 EXPECT_EQ(iter, nullptr);
216 }
217