| 1 |
/* Movie.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2011-2012,2014-2018,2022,2024 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 |
|
| 19 |
#include "Movie.h" |
| 20 |
|
| 21 |
#include "oo_DESTROY.h" |
| 22 |
#include "Movie_def.h" |
| 23 |
#include "oo_COPY.h" |
| 24 |
#include "Movie_def.h" |
| 25 |
#include "oo_EQUAL.h" |
| 26 |
#include "Movie_def.h" |
| 27 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 28 |
#include "Movie_def.h" |
| 29 |
#include "oo_WRITE_TEXT.h" |
| 30 |
#include "Movie_def.h" |
| 31 |
#include "oo_READ_TEXT.h" |
| 32 |
#include "Movie_def.h" |
| 33 |
#include "oo_WRITE_BINARY.h" |
| 34 |
#include "Movie_def.h" |
| 35 |
#include "oo_READ_BINARY.h" |
| 36 |
#include "Movie_def.h" |
| 37 |
#include "oo_DESCRIPTION.h" |
| 38 |
#include "Movie_def.h" |
| 39 |
|
| 40 |
Thing_implement (Movie, Sampled, 0); |
| 41 |
|
| 42 |
void structMovie :: v1_info () { |
| 43 |
structDaata :: v1_info (); |
| 44 |
MelderInfo_writeLine (U"Start time: ", xmin, U" seconds"); |
| 45 |
MelderInfo_writeLine (U"End time: ", xmax, U" seconds"); |
| 46 |
MelderInfo_writeLine (U"Total duration: ", xmax - xmin, U" seconds"); |
| 47 |
MelderInfo_writeLine (U"Time sampling:"); |
| 48 |
MelderInfo_writeLine (U" Number of frames: ", nx); |
| 49 |
MelderInfo_writeLine (U" Frame duration: ", dx, U" seconds"); |
| 50 |
MelderInfo_writeLine (U" Frame rate: ", Melder_single (1.0 / dx), U" frames per second"); |
| 51 |
MelderInfo_writeLine (U" First frame centred at: ", x1, U" seconds"); |
| 52 |
} |
| 53 |
|
| 54 |
void Movie_init (Movie me, autoSound sound, conststring32 folderName, autoStrings fileNames) |
| 55 |
{ |
| 56 |
Sampled_init (me, sound -> xmin, sound -> xmax, fileNames ? fileNames -> numberOfStrings : 0, 0.04, 0.0); |
| 57 |
my d_sound = sound.move(); |
| 58 |
my d_folderName = Melder_dup (folderName); |
| 59 |
my d_fileNames = fileNames.move(); |
| 60 |
} |
| 61 |
|
| 62 |
autoMovie Movie_openFromSoundFile (MelderFile file) |
| 63 |
{ |
| 64 |
try { |
| 65 |
autoMovie me = Thing_new (Movie); |
| 66 |
autoSound sound = Sound_readFromSoundFile (file); |
| 67 |
autoMelderString fileNameHead; |
| 68 |
MelderString_copy (& fileNameHead, MelderFile_peekPath (file)); |
| 69 |
char32 *extensionLocation = str32rchr (fileNameHead.string, U'.'); |
| 70 |
if (! extensionLocation) |
| 71 |
extensionLocation = & fileNameHead.string [fileNameHead.length]; |
| 72 |
*extensionLocation = U'\0'; |
| 73 |
fileNameHead.length = extensionLocation - fileNameHead.string; |
| 74 |
autoStrings strings = Strings_createAsFileList (Melder_cat (fileNameHead.string, U"*.png")); |
| 75 |
structMelderFolder folder { }; |
| 76 |
MelderFile_getParentFolder (file, & folder); |
| 77 |
Movie_init (me.get(), sound.move(), MelderFolder_peekPath (& folder), strings.move()); |
| 78 |
return me; |
| 79 |
} catch (MelderError) { |
| 80 |
Melder_throw (U"Movie object not read from file ", file, U"."); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
void Movie_paintOneImageInside (Movie me, Graphics graphics, integer frameNumber, double xmin, double xmax, double ymin, double ymax) |
| 85 |
{ |
| 86 |
try { |
| 87 |
if (frameNumber < 1) |
| 88 |
Melder_throw (U"Specified frame number is ", frameNumber, U" but should be at least 1."); |
| 89 |
if (frameNumber > my nx) |
| 90 |
Melder_throw (U"Specified frame number is ", frameNumber, U" but there are only ", my nx, U"frames."); |
| 91 |
Melder_assert (my d_fileNames); |
| 92 |
Melder_assert (my d_fileNames -> numberOfStrings == my nx); |
| 93 |
structMelderFolder folder { }; |
| 94 |
Melder_pathToFolder (my d_folderName.get(), & folder); |
| 95 |
structMelderFile file { }; |
| 96 |
MelderFolder_getFile (& folder, my d_fileNames -> strings [frameNumber].get(), & file); |
| 97 |
Graphics_imageFromFile (graphics, MelderFile_peekPath (& file), xmin, xmax, ymin, ymax); |
| 98 |
} catch (MelderError) { |
| 99 |
Melder_throw (me, U": image ", frameNumber, U" not painted."); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
void Movie_paintOneImage (Movie me, Graphics graphics, integer frameNumber, double xmin, double xmax, double ymin, double ymax) { |
| 104 |
try { |
| 105 |
Graphics_setInner (graphics); |
| 106 |
Graphics_setWindow (graphics, 0.0, 1.0, 0.0, 1.0); |
| 107 |
Movie_paintOneImageInside (me, graphics, frameNumber, xmin, xmax, ymin, ymax); |
| 108 |
Graphics_unsetInner (graphics); |
| 109 |
} catch (MelderError) { |
| 110 |
Graphics_unsetInner (graphics); // TODO: should be auto |
| 111 |
throw; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void Movie_play (Movie me, Graphics /* g */, double tmin, double tmax, Sound_PlayCallback playCallback, Thing playBoss) { |
| 116 |
Sound_playPart (my d_sound.get(), tmin, tmax, playCallback, playBoss); |
| 117 |
} |
| 118 |
|
| 119 |
/* End of file Movie.cpp */ |