| 36 | } |
| 37 | |
| 38 | void Compute(OpKernelContext* context) override { |
| 39 | const Tensor& contents = context->input(0); |
| 40 | OP_REQUIRES(context, TensorShapeUtils::IsScalar(contents.shape()), |
| 41 | errors::InvalidArgument("contents must be scalar, got shape ", |
| 42 | contents.shape().DebugString())); |
| 43 | const string& wav_string = contents.scalar<tstring>()(); |
| 44 | OP_REQUIRES(context, wav_string.size() <= std::numeric_limits<int>::max(), |
| 45 | errors::InvalidArgument("WAV contents are too large for int: ", |
| 46 | wav_string.size())); |
| 47 | |
| 48 | std::vector<float> decoded_samples; |
| 49 | uint32 decoded_sample_count; |
| 50 | uint16 decoded_channel_count; |
| 51 | uint32 decoded_sample_rate; |
| 52 | OP_REQUIRES_OK(context, |
| 53 | wav::DecodeLin16WaveAsFloatVector( |
| 54 | wav_string, &decoded_samples, &decoded_sample_count, |
| 55 | &decoded_channel_count, &decoded_sample_rate)); |
| 56 | |
| 57 | int32 output_sample_count; |
| 58 | if (desired_samples_ == -1) { |
| 59 | output_sample_count = decoded_sample_count; |
| 60 | } else { |
| 61 | output_sample_count = desired_samples_; |
| 62 | } |
| 63 | int32 output_channel_count; |
| 64 | if (desired_channels_ == -1) { |
| 65 | output_channel_count = decoded_channel_count; |
| 66 | } else { |
| 67 | output_channel_count = desired_channels_; |
| 68 | } |
| 69 | |
| 70 | Tensor* output = nullptr; |
| 71 | OP_REQUIRES_OK( |
| 72 | context, |
| 73 | context->allocate_output( |
| 74 | 0, TensorShape({output_sample_count, output_channel_count}), |
| 75 | &output)); |
| 76 | |
| 77 | auto output_matrix = output->matrix<float>(); |
| 78 | for (int sample = 0; sample < output_sample_count; ++sample) { |
| 79 | for (int channel = 0; channel < output_channel_count; ++channel) { |
| 80 | float output_value; |
| 81 | if (sample >= decoded_sample_count) { |
| 82 | output_value = 0.0f; |
| 83 | } else { |
| 84 | int source_channel; |
| 85 | if (channel < decoded_channel_count) { |
| 86 | source_channel = channel; |
| 87 | } else { |
| 88 | source_channel = decoded_channel_count - 1; |
| 89 | } |
| 90 | const int decoded_index = |
| 91 | (sample * decoded_channel_count) + source_channel; |
| 92 | output_value = decoded_samples[decoded_index]; |
| 93 | } |
| 94 | output_matrix(sample, channel) = output_value; |
| 95 | } |
nothing calls this directly
no test coverage detected