* Encoding implementation, shared across V1 and V2 ops. Creates a new * output in the context. */
| 29 | * output in the context. |
| 30 | */ |
| 31 | void Encode(OpKernelContext* context, const Tensor& contents, |
| 32 | const string& file_format, const int32 bits_per_second, |
| 33 | const int32 samples_per_second) { |
| 34 | std::vector<float> samples; |
| 35 | samples.reserve(contents.NumElements()); |
| 36 | for (int32 i = 0; i < contents.NumElements(); ++i) { |
| 37 | samples.push_back(contents.flat<float>()(i)); |
| 38 | } |
| 39 | const int32 channel_count = contents.dim_size(1); |
| 40 | string encoded_audio; |
| 41 | OP_REQUIRES_OK( |
| 42 | context, CreateAudioFile(file_format, bits_per_second, samples_per_second, |
| 43 | channel_count, samples, &encoded_audio)); |
| 44 | |
| 45 | // Copy the encoded audio file to the output tensor. |
| 46 | Tensor* output = nullptr; |
| 47 | OP_REQUIRES_OK(context, context->allocate_output(0, TensorShape(), &output)); |
| 48 | output->scalar<tstring>()() = encoded_audio; |
| 49 | } |
| 50 | |
| 51 | } // namespace |
| 52 |
no test coverage detected