WIP: parakeeet
This commit is contained in:
@@ -13,16 +13,53 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import logging
|
||||
import typing
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
import gi
|
||||
import numpy as np
|
||||
from gi.repository import Gio, GObject
|
||||
|
||||
try:
|
||||
gi.require_version('Gst', '1.0')
|
||||
from gi.repository import Gst
|
||||
except Exception:
|
||||
if typing.TYPE_CHECKING:
|
||||
from gi.repository import Gst
|
||||
|
||||
log = logging.getLogger('gajim.p.sttvm_helper')
|
||||
|
||||
|
||||
@dataclass
|
||||
class Results:
|
||||
text: str
|
||||
|
||||
|
||||
def load_audio(path: Path, sample_rate: int = 16000) -> np.ndarray:
|
||||
Gst.init(None)
|
||||
pipeline = Gst.parse_launch(
|
||||
'filesrc name=src ! decodebin ! audioconvert ! audioresample ! '
|
||||
f'audio/x-raw,format=F32LE,rate={sample_rate},channels=1 ! '
|
||||
'appsink name=sink sync=false'
|
||||
)
|
||||
pipeline.get_by_name('src').set_property('location', str(path))
|
||||
sink = pipeline.get_by_name('sink')
|
||||
chunks: list[np.ndarray] = []
|
||||
|
||||
pipeline.set_state(Gst.State.PLAYING)
|
||||
while (sample := sink.emit('try-pull-sample', 10 * Gst.SECOND)) is not None:
|
||||
buf = sample.get_buffer()
|
||||
_, info = buf.map(Gst.MapFlags.READ)
|
||||
chunks.append(np.frombuffer(bytes(info.data), dtype=np.float32))
|
||||
buf.unmap(info)
|
||||
pipeline.set_state(Gst.State.NULL)
|
||||
|
||||
if not chunks:
|
||||
raise RuntimeError(f'Could not decode audio: {path}')
|
||||
return np.concatenate(chunks)
|
||||
|
||||
|
||||
'''
|
||||
https://discourse.gnome.org/t/gtk-threading-problem-with-glib-idle-add/13597/5
|
||||
@@ -57,6 +94,7 @@ class BackgroundTask(GObject.Object):
|
||||
retval = self.function()
|
||||
task.return_value(retval)
|
||||
except Exception as e:
|
||||
log.exception('Background task failed')
|
||||
task.return_value(e)
|
||||
|
||||
def finish(self):
|
||||
|
||||
Reference in New Issue
Block a user