adam-codersgu / supernova

A Kotlin-based Android application that allows the user to play music from their device while custom animations fall over the album artwork of the currently playing song.
0 stars 0 forks source link

Investigate whether the compression of album artwork for metadata packaging can be removed #18

Closed adam-codersgu closed 1 year ago

adam-codersgu commented 1 year ago

Currently, the MediaPlaybackService compresses album artwork using various methods for packaging in the currently active metadata. The purpose of this ticket is to investigate whether this is really necessary (i.e. are there any noticeable performance effects from not doing the compression). If no performance impacts are observed, then the extra methods can be removed. The METADATA_KEY_ALBUM_ART_URI key could also be removed from the metatdata bundle, and the ControlsFragment and CurrentlyPlayingFragment classes can get the metadata bitmap directly, rather than using MainActivity methods. Once the above has been implemented, it should also be checked whether the MainActivity methods that were previously helping with the processing can be removed also.

adam-codersgu commented 1 year ago

Working on branch enhancement/do_not_compress_artwork

adam-codersgu commented 1 year ago

Deleted code from MediaPlaybackService:

/**
     * Retrieve the album artwork for a given album ID. If no artwork is found,
     * then a default artwork image is returned instead.
     *
     * @param albumId - The ID of the album that artwork should be retrieved for.
     * @return A Bitmap representation of the album artwork.
     */
    private fun getArtworkAsBitmap(albumId: String?) : Bitmap {
        albumId?.let {
            try {
                return BitmapFactory.Options().run {
                    inJustDecodeBounds = true
                    val contextWrapper = ContextWrapper(applicationContext)
                    val imageDirectory = contextWrapper.getDir("albumArt", Context.MODE_PRIVATE)
                    val imageFile = File(imageDirectory, "$it.jpg")
                    BitmapFactory.decodeStream(FileInputStream(imageFile))

                    inSampleSize = calculateInSampleSize(this)
                    inJustDecodeBounds = false
                    BitmapFactory.decodeStream(FileInputStream(imageFile))
                }
            } catch (_: Exception) { }
        }
        // If an error has occurred or the album ID is null, then return a default artwork image
        return BitmapFactory.decodeResource(applicationContext.resources, R.drawable.no_album_artwork)
    }

    /**
     * Calculate an inSampleSize value that can be used to scale the
     * dimensions of a Bitmap image. Useful for compressing large images.
     *
     * @param options - The BitmapFactory.Options instance the should be used to
     * calculate the inSampleSize value for.
     * @return An integer inSampleSize value.
     */
    private fun calculateInSampleSize(options: BitmapFactory.Options): Int {
        val reqWidth = 100; val reqHeight = 100
        val (height: Int, width: Int) = options.run { outHeight to outWidth }
        var inSampleSize = 1

        if (height > reqHeight || width > reqWidth) {
            val halfHeight = height / 2; val halfWidth = width / 2

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
                inSampleSize *= 2
            }
        }

        return inSampleSize
    }
adam-codersgu commented 1 year ago

Performance testing results included below. If anything the changes have made the notification loading process faster:

-- BEFORE -- Time to load metadata for songs 503835104 501681614 506484427 506512135 514680209

2022-12-04 21:57:25.688 14012-14012/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 4943854 2022-12-04 21:57:25.708 14012-14012/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 16249584 2022-12-04 21:57:31.830 14012-14012/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 8839792 2022-12-04 21:57:32.429 14012-14012/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 7532448 2022-12-04 21:57:38.235 14012-14012/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 9037604 2022-12-04 21:57:38.764 14012-14012/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 7553958 2022-12-04 21:57:39.757 14012-14012/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 8300573 2022-12-04 21:57:40.524 14012-14012/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 7803854

-- AFTER 2022-12-04 22:16:17.161 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 5627552 2022-12-04 22:16:17.168 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 5469479 2022-12-04 22:16:19.950 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 9087396 2022-12-04 22:16:20.664 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 6635938 2022-12-04 22:16:21.389 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 6878386 2022-12-04 22:16:22.079 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 11425313 2022-12-04 22:16:22.873 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 7353229 2022-12-04 22:16:23.815 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 7446979 2022-12-04 22:16:24.813 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 7544531 2022-12-04 22:16:25.937 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 6403281 2022-12-04 22:16:28.045 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for loading 'Cause I Love You was 284544740 2022-12-04 22:16:28.162 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 5666771 2022-12-04 22:16:28.170 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 6244479 2022-12-04 22:16:32.123 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for loading The Loving Gift was 277556511 2022-12-04 22:16:32.241 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 5898125 2022-12-04 22:16:32.248 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 5577812 2022-12-04 22:16:35.623 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for loading Help Me Make It Through the Night was 279719219 2022-12-04 22:16:35.727 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 5673594 2022-12-04 22:16:35.736 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 6710989 2022-12-04 22:16:41.767 15808-15808/com.codersguidebook.supernova E/DEBUGGING: The time elapsed for the notification was 7703750

adam-codersgu commented 1 year ago

Completed and merged https://github.com/adam-codersgu/supernova/pull/40