# Development notes:
# - To build the snap run `snapcraft clean` and `snapcraft pack --verbose`
# - To install local snap `snap install ./buzz_*.snap --dangerous`
name: buzz
base: core24
version: git
title: Buzz
summary: Buzz, offline audio transcription and translation
website: https://buzzcaptions.com
source-code: https://github.com/chidiwilliams/buzz
issues: https://github.com/chidiwilliams/buzz/issues
contact: https://github.com/chidiwilliams
description: |
  Buzz transcribes and translates audio to text offline using OpenAI's Whisper.
  Import audio and video files into Buzz and export them as TXT, SRT, or VTT files.
  Buzz supports Whisper, Whisper.cpp, Faster Whisper, Whisper-compatible models
  from the Hugging Face repository, and the OpenAI Whisper API.
grade: stable
confinement: strict
license: MIT
icon: buzz/assets/buzz.svg

platforms:
  amd64:

parts:
  alsa-pulseaudio:
    plugin: dump
    source: .
    override-pull: |
      mkdir etc -p
      cat > etc/asound.conf <<EOF
      pcm.!default {
          type pulse
          fallback "sysdefault"
          hint {
              show on
              description "Default ALSA Output (currently PulseAudio Sound Server)"
          }
      }
      ctl.!default {
          type pulse
          fallback "sysdefault"
      }
      EOF
    organize:
      etc/asound.conf: etc/asound.conf
    stage:
      - etc/asound.conf
    prime:
      - etc/asound.conf

  portaudio:
    after: [ alsa-pulseaudio ]
    plugin: autotools
    source: https://files.portaudio.com/archives/pa_stable_v190700_20210406.tgz
    build-packages:
      - libasound2-dev
      - libpulse-dev
    autotools-configure-parameters:
      - --enable-shared
      - --disable-static
    stage:
      - usr/local/lib/libportaudio*
    prime:
      - usr/local/lib/libportaudio*

  buzz:
    after: [ alsa-pulseaudio, portaudio ]
    plugin: uv
    source: .
    build-snaps:
      - astral-uv
    stage-snaps:
      - astral-uv
    build-packages:
      - wget
      - portaudio19-dev
      - qt6-declarative-dev
      - qt6-multimedia-dev
      - libvulkan-dev
      - ccache
      - cmake
      - python3
    build-environment:
      - UV_PYTHON: python3
      - UV_NO_CACHE: "true"
      - UV_HTTP_TIMEOUT: "120"
    stage-packages:
      - python3
      - libgcc-s1
      - libc6
      - libproxy1v5
      # Audio
      - ffmpeg
      - libpulse0
      - libasound2t64
      - libasound2-dev
      - libasound2-plugins
      - libasound2-plugins-extra
      - libyaml-dev
      - liboss4-salsa2
      # Display
      - libxkbcommon-x11-0
      - libxcb-icccm4
      - libxcb-image0
      - libxcb-keysyms1
      - libxcb-randr0
      - libxcb-render-util0
      - libxcb-xinerama0
      - libxcb-shape0
      - libxcb-cursor0
      # GPU
      - libglu1-mesa
      - libvulkan1
      - mesa-vulkan-drivers
    override-build: |
      wget -qO- https://packages.lunarg.com/lunarg-signing-key-pub.asc | tee /etc/apt/trusted.gpg.d/lunarg.asc
      wget -qO /etc/apt/sources.list.d/lunarg-vulkan-noble.list http://packages.lunarg.com/vulkan/lunarg-vulkan-noble.list
      apt update
      apt install -y vulkan-sdk

      # Clear cache to save space on CI
      apt clean

      # Run default build steps
      craftctl default

      # Clean caches
      uv cache clean

      # Create launcher wrapper to ensure the snap's own portaudio and libasound are found
      # before gnome content snap libraries (which desktop-launch prepends to LD_LIBRARY_PATH)
      mkdir -p $CRAFT_PART_INSTALL/bin
      printf '#!/bin/sh\nexport LD_LIBRARY_PATH="$SNAP/usr/local/lib:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH"\nexec "$SNAP/bin/python" -m buzz "$@"\n' > $CRAFT_PART_INSTALL/bin/buzz-launcher
      chmod +x $CRAFT_PART_INSTALL/bin/buzz-launcher

      # Copy source files
      cp -r $CRAFT_PART_BUILD/buzz $CRAFT_PART_INSTALL/
      cp -r $CRAFT_PART_BUILD/ctc_forced_aligner $CRAFT_PART_INSTALL/
      cp -r $CRAFT_PART_BUILD/deepmultilingualpunctuation $CRAFT_PART_INSTALL/
      cp -r $CRAFT_PART_BUILD/demucs_repo $CRAFT_PART_INSTALL/
      cp -r $CRAFT_PART_BUILD/whisper_diarization $CRAFT_PART_INSTALL/

      # Create desktop file
      mkdir -p $CRAFT_PART_INSTALL/usr/share/applications
      cp $CRAFT_PART_BUILD/buzz.desktop $CRAFT_PART_INSTALL/usr/share/applications/
    stage:
      - '*'
    prime:
      - '*'

  gpu-2404:
    after: [ buzz ]
    source: https://github.com/canonical/gpu-snap.git
    plugin: dump
    override-prime: |
      craftctl default
      ${CRAFT_PART_SRC}/bin/gpu-2404-cleanup mesa-2404
      # Workaround for https://bugs.launchpad.net/snapd/+bug/2055273
      mkdir -p "${CRAFT_PRIME}/gpu-2404"
    prime:
      - bin/gpu-2404-wrapper

apps:
  buzz:
    extensions:
      - gnome
    command-chain:
      - bin/gpu-2404-wrapper
    command: snap/command-chain/desktop-launch $SNAP/bin/buzz-launcher
    desktop: usr/share/applications/buzz.desktop
    environment:
      PATH: $SNAP/usr/bin:$SNAP/bin:$PATH
      LD_LIBRARY_PATH: $SNAP/usr/local/lib:$SNAP/lib/python3.12/site-packages/nvidia/cudnn/lib:$SNAP/lib/python3.12/site-packages/PyQt6:$SNAP/lib/python3.12/site-packages/PyQt6/Qt6/lib:$SNAP/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/lapack:$SNAP/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/blas:$SNAP/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/oss4-libsalsa:$SNAP/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/libproxy:$SNAP/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/alsa-lib:$SNAP:$LD_LIBRARY_PATH
      PYTHONPATH: $SNAP:$SNAP/lib/python3.12/site-packages/PyQt6:$SNAP/lib/python3.12/site-packages/PyQt6/Qt6/lib:$SNAP/usr/lib/python3/dist-packages:$SNAP/usr/lib/python3.12/site-packages:$SNAP/usr/local/lib/python3.12/dist-packages:$SNAP/usr/lib/python3.12/dist-packages:$PYTHONPATH
      QT_MEDIA_BACKEND: ffmpeg
      PULSE_LATENCY_MSEC: "30"
      ALSA_CONFIG_PATH: $SNAP/etc/asound.conf
    plugs:
      - x11
      - unity7
      - wayland
      - home
      - network
      - network-bind
      - desktop
      - desktop-legacy
      - gsettings
      - opengl
      - removable-media
      - audio-playback
      - audio-record
      # Fallback for keyring support if secrets portal is missing, user has to connect this manually
      - password-manager-service

layout:
  /usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/alsa-lib:
    bind: $SNAP/usr/lib/$CRAFT_ARCH_TRIPLET_BUILD_FOR/alsa-lib
