Movie.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Movie.cpp
  2. *
  3. * Copyright (C) 2011-2012,2015,2016,2017 Paul Boersma
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "Movie.h"
  19. #include "oo_DESTROY.h"
  20. #include "Movie_def.h"
  21. #include "oo_COPY.h"
  22. #include "Movie_def.h"
  23. #include "oo_EQUAL.h"
  24. #include "Movie_def.h"
  25. #include "oo_CAN_WRITE_AS_ENCODING.h"
  26. #include "Movie_def.h"
  27. #include "oo_WRITE_TEXT.h"
  28. #include "Movie_def.h"
  29. #include "oo_READ_TEXT.h"
  30. #include "Movie_def.h"
  31. #include "oo_WRITE_BINARY.h"
  32. #include "Movie_def.h"
  33. #include "oo_READ_BINARY.h"
  34. #include "Movie_def.h"
  35. #include "oo_DESCRIPTION.h"
  36. #include "Movie_def.h"
  37. Thing_implement (Movie, Sampled, 0);
  38. void structMovie :: v_info ()
  39. {
  40. structDaata :: v_info ();
  41. MelderInfo_writeLine (U"Start time: ", xmin, U" seconds");
  42. MelderInfo_writeLine (U"End time: ", xmax, U" seconds");
  43. MelderInfo_writeLine (U"Total duration: ", xmax - xmin, U" seconds");
  44. MelderInfo_writeLine (U"Time sampling:");
  45. MelderInfo_writeLine (U" Number of frames: ", nx);
  46. MelderInfo_writeLine (U" Frame duration: ", dx, U" seconds");
  47. MelderInfo_writeLine (U" Frame rate: ", Melder_single (1.0 / dx), U" frames per second");
  48. MelderInfo_writeLine (U" First frame centred at: ", x1, U" seconds");
  49. }
  50. void Movie_init (Movie me, autoSound sound, conststring32 folderName, autoStrings fileNames)
  51. {
  52. Sampled_init (me, sound -> xmin, sound -> xmax, fileNames ? fileNames -> numberOfStrings : 0, 0.04, 0.0);
  53. my d_sound = sound.move();
  54. my d_folderName = Melder_dup (folderName);
  55. my d_fileNames = fileNames.move();
  56. }
  57. autoMovie Movie_openFromSoundFile (MelderFile file)
  58. {
  59. try {
  60. autoMovie me = Thing_new (Movie);
  61. autoSound sound = Sound_readFromSoundFile (file);
  62. autoMelderString fileNameHead;
  63. MelderString_copy (& fileNameHead, Melder_fileToPath (file));
  64. char32 *extensionLocation = str32rchr (fileNameHead.string, U'.');
  65. if (! extensionLocation)
  66. extensionLocation = & fileNameHead.string [fileNameHead.length];
  67. *extensionLocation = U'\0';
  68. fileNameHead.length = extensionLocation - fileNameHead.string;
  69. autoStrings strings = Strings_createAsFileList (Melder_cat (fileNameHead.string, U"*.png"));
  70. structMelderDir folder { };
  71. MelderFile_getParentDir (file, & folder);
  72. Movie_init (me.get(), sound.move(), Melder_dirToPath (& folder), strings.move());
  73. return me;
  74. } catch (MelderError) {
  75. Melder_throw (U"Movie object not read from file ", file, U".");
  76. }
  77. }
  78. void Movie_paintOneImageInside (Movie me, Graphics graphics, integer frameNumber, double xmin, double xmax, double ymin, double ymax)
  79. {
  80. try {
  81. if (frameNumber < 1) Melder_throw (U"Specified frame number is ", frameNumber, U" but should be at least 1.");
  82. if (frameNumber > my nx) Melder_throw (U"Specified frame number is ", frameNumber, U" but there are only ", my nx, U"frames.");
  83. Melder_assert (my d_fileNames);
  84. Melder_assert (my d_fileNames -> numberOfStrings == my nx);
  85. structMelderDir folder { };
  86. Melder_pathToDir (my d_folderName.get(), & folder);
  87. structMelderFile file { };
  88. MelderDir_getFile (& folder, my d_fileNames -> strings [frameNumber].get(), & file);
  89. Graphics_imageFromFile (graphics, Melder_fileToPath (& file), xmin, xmax, ymin, ymax);
  90. } catch (MelderError) {
  91. Melder_throw (me, U": image ", frameNumber, U" not painted.");
  92. }
  93. }
  94. void Movie_paintOneImage (Movie me, Graphics graphics, integer frameNumber, double xmin, double xmax, double ymin, double ymax) {
  95. try {
  96. Graphics_setInner (graphics);
  97. Graphics_setWindow (graphics, 0.0, 1.0, 0.0, 1.0);
  98. Movie_paintOneImageInside (me, graphics, frameNumber, xmin, xmax, ymin, ymax);
  99. Graphics_unsetInner (graphics);
  100. } catch (MelderError) {
  101. Graphics_unsetInner (graphics); // TODO: should be auto
  102. throw;
  103. }
  104. }
  105. void Movie_play (Movie me, Graphics /* g */, double tmin, double tmax, Sound_PlayCallback callback, Thing boss)
  106. {
  107. Sound_playPart (my d_sound.get(), tmin, tmax, callback, boss);
  108. }
  109. /* End of file Movie.cpp */