| 37 | } |
| 38 | |
| 39 | void Compute(OpKernelContext* c) override { |
| 40 | const Tensor& tag = c->input(0); |
| 41 | const Tensor& tensor = c->input(1); |
| 42 | OP_REQUIRES(c, IsLegacyScalar(tag.shape()), |
| 43 | errors::InvalidArgument("Tag must be a scalar")); |
| 44 | OP_REQUIRES(c, tensor.dims() >= 2 && tensor.dims() <= 3, |
| 45 | errors::InvalidArgument("Tensor must be 3-D or 2-D, got: ", |
| 46 | tensor.shape().DebugString())); |
| 47 | const string& base_tag = tag.scalar<tstring>()(); |
| 48 | |
| 49 | float sample_rate = sample_rate_attr_; |
| 50 | if (!has_sample_rate_attr_) { |
| 51 | const Tensor& sample_rate_tensor = c->input(2); |
| 52 | sample_rate = sample_rate_tensor.scalar<float>()(); |
| 53 | } |
| 54 | OP_REQUIRES(c, sample_rate > 0.0f, |
| 55 | errors::InvalidArgument("sample_rate must be > 0")); |
| 56 | |
| 57 | const int batch_size = tensor.dim_size(0); |
| 58 | const int64 length_frames = tensor.dim_size(1); |
| 59 | const int64 num_channels = |
| 60 | tensor.dims() == 2 ? 1 : tensor.dim_size(tensor.dims() - 1); |
| 61 | |
| 62 | Summary s; |
| 63 | const int N = std::min<int>(max_outputs_, batch_size); |
| 64 | for (int i = 0; i < N; ++i) { |
| 65 | Summary::Value* v = s.add_value(); |
| 66 | if (max_outputs_ > 1) { |
| 67 | v->set_tag(strings::StrCat(base_tag, "/audio/", i)); |
| 68 | } else { |
| 69 | v->set_tag(strings::StrCat(base_tag, "/audio")); |
| 70 | } |
| 71 | |
| 72 | Summary::Audio* sa = v->mutable_audio(); |
| 73 | sa->set_sample_rate(sample_rate); |
| 74 | sa->set_num_channels(num_channels); |
| 75 | sa->set_length_frames(length_frames); |
| 76 | sa->set_content_type("audio/wav"); |
| 77 | |
| 78 | auto values = |
| 79 | tensor.shaped<float, 3>({batch_size, length_frames, num_channels}); |
| 80 | auto channels_by_frames = typename TTypes<float>::ConstMatrix( |
| 81 | &values(i, 0, 0), |
| 82 | Eigen::DSizes<Eigen::DenseIndex, 2>(length_frames, num_channels)); |
| 83 | size_t sample_rate_truncated = lrintf(sample_rate); |
| 84 | if (sample_rate_truncated == 0) { |
| 85 | sample_rate_truncated = 1; |
| 86 | } |
| 87 | OP_REQUIRES_OK( |
| 88 | c, wav::EncodeAudioAsS16LEWav( |
| 89 | channels_by_frames.data(), sample_rate_truncated, num_channels, |
| 90 | length_frames, sa->mutable_encoded_audio_string())); |
| 91 | } |
| 92 | |
| 93 | Tensor* summary_tensor = nullptr; |
| 94 | OP_REQUIRES_OK(c, c->allocate_output(0, TensorShape({}), &summary_tensor)); |
| 95 | CHECK(SerializeToTString(s, &summary_tensor->scalar<tstring>()())); |
| 96 | } |
nothing calls this directly
no test coverage detected