kaldi-utils.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // base/kaldi-utils.cc
  2. // Copyright 2009-2011 Karel Vesely; Yanmin Qian; Microsoft Corporation
  3. // See ../../COPYING for clarification regarding multiple authors
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  10. // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  11. // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. // MERCHANTABLITY OR NON-INFRINGEMENT.
  13. // See the Apache 2 License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "base/kaldi-utils.h"
  16. #include <cctype>
  17. #include <chrono>
  18. #include <cstdio>
  19. #include <thread>
  20. namespace kaldi {
  21. std::string CharToString(const char &c) {
  22. char buf[20];
  23. if (std::isprint(c))
  24. std::snprintf(buf, sizeof(buf), "\'%c\'", c);
  25. else
  26. std::snprintf(buf, sizeof(buf), "[character %d]", static_cast<int>(c));
  27. return buf;
  28. }
  29. void Sleep(double sec) {
  30. // duration_cast<> rounds down, add 0.5 to compensate.
  31. auto dur_nanos = std::chrono::duration<double, std::nano>(sec * 1E9 + 0.5);
  32. auto dur_syshires = std::chrono::duration_cast<
  33. typename std::chrono::high_resolution_clock::duration>(dur_nanos);
  34. std::this_thread::sleep_for(dur_syshires);
  35. }
  36. } // end namespace kaldi