MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / Compute

Method Compute

tensorflow/core/kernels/decode_wav_op.cc:38–102  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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 }

Callers

nothing calls this directly

Calls 10

InvalidArgumentFunction · 0.85
allocate_outputMethod · 0.80
IsScalarFunction · 0.50
maxFunction · 0.50
TensorShapeClass · 0.50
inputMethod · 0.45
shapeMethod · 0.45
DebugStringMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected