Skip to content

Commit

Permalink
Fix livepng not animating
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoCaracciolo committed Nov 20, 2024
1 parent c773213 commit 7ba5547
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
2 changes: 1 addition & 1 deletion modules/python3-g4f.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "python3-g4f",
"buildsystem": "simple",
"build-commands": [
"pip3 install --verbose --prefix=${FLATPAK_DEST} g4f==0.3.3.3 curl_cffi pillow"
"pip3 install --verbose --prefix=${FLATPAK_DEST} g4f==0.3.3.4 curl_cffi pillow"
],
"build-options": {
"build-args": [
Expand Down
16 changes: 7 additions & 9 deletions modules/python3-pygame.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
"name": "python3-pygame",
"buildsystem": "simple",
"build-commands": [
"pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"pygame\" --no-build-isolation"
"pip3 install --verbose --prefix=${FLATPAK_DEST} pygame"
],
"sources": [
{
"type": "file",
"url": "https://files.pythonhosted.org/packages/49/cc/08bba60f00541f62aaa252ce0cfbd60aebd04616c0b9574f755b583e45ae/pygame-2.6.1.tar.gz",
"sha256": "56fb02ead529cee00d415c3e007f75e0780c655909aaa8e8bf616ee09c9feb1f"
}
]
}
"build-options": {
"build-args": [
"--share=network"
]
}
}
52 changes: 41 additions & 11 deletions src/avatar.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from abc import abstractmethod
from os.path import abspath, isdir, isfile
from typing import Any
from gi.repository import Gtk, WebKit, GLib, GdkPixbuf
from livepng.model import Semaphore
from typing import Any, overload
from gi.repository import Gtk, WebKit, GLib, GdkPixbuf, Gdk

from .translator import TranslatorHandler

Expand All @@ -11,7 +9,6 @@
import os, subprocess, threading, json
from http.server import HTTPServer, SimpleHTTPRequestHandler
from livepng import LivePNG
from livepng.validator import ModelValidator
from livepng.constants import FilepathOutput
from pydub import AudioSegment
from time import sleep
Expand Down Expand Up @@ -367,11 +364,42 @@ def get_available_models(self) -> list[tuple[str, str]]:
return result

def create_gtk_widget(self) -> Gtk.Widget:
self.image = Gtk.Picture()
self.image.set_vexpand(True)
self.image.set_hexpand(True)
overlay = Gtk.Overlay()
overlay.set_hexpand(True)
overlay.set_vexpand(True)
self.drawing_area = Gtk.DrawingArea()
self.drawing_area.set_hexpand(True)
self.drawing_area.set_vexpand(True)
def on_draw(widget, cr, width, height):
if self.pixbuf is None:
return
# Get the original dimensions of the image
original_width = self.pixbuf.get_width()
original_height = self.pixbuf.get_height()

# Calculate the scaling factors while maintaining the aspect ratio
if original_width / original_height > width / height:
# Image is wider than the container
scale_factor = width / original_width
else:
# Image is taller than the container
scale_factor = height / original_height

# Calculate the new dimensions
new_width = int(original_width * scale_factor)
new_height = int(original_height * scale_factor)

# Scale the image to fit the drawing area
scaled_pixbuf = self.pixbuf.scale_simple(new_width, new_height, GdkPixbuf.InterpType.BILINEAR)

x = (width - new_width) / 2
y = (height - new_height) / 2
# Draw the scaled image
Gdk.cairo_set_source_pixbuf(cr, scaled_pixbuf, x, y)
cr.paint()
self.drawing_area.set_draw_func(on_draw)
self.__load_model()
return self.image
return self.drawing_area

def set_expression(self, expression: str):
self.model.set_current_expression(expression)
Expand Down Expand Up @@ -405,9 +433,11 @@ def __load_model(self):

def __on_update(self, frame:str):
if frame in self.cachedpixbuf:
GLib.idle_add(self.image.set_pixbuf, self.cachedpixbuf[frame])
pix = self.cachedpixbuf[frame]
else:
GLib.idle_add(self.image.set_pixbuf, self.__load_image(frame))
pix = self.__load_image(frame)
self.pixbuf = pix
self.drawing_area.queue_draw()

def preacache_images(self):
self.cachedpixbuf = {}
Expand Down
1 change: 1 addition & 0 deletions src/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def playsound(self, path):
self.stop()
self._play_lock.acquire()
self.on_start()
print(path)
mixer.music.load(path)
mixer.music.play()
while mixer.music.get_busy():
Expand Down

0 comments on commit 7ba5547

Please sign in to comment.