<commit_before>child_process = require 'child_process'
crypto = require 'crypto'
fs = require 'fs'
path = require 'path'
temp = require 'temp'

# 1 minute of 160 kbps music.
FAILED_CONVERSION_IS_OK_SIZE = 60 * 160 * 1000 / 8

class AudioConverter
        _conversionDone: (target, code, callback) =>
                if code != 0
                        # We have failed file conversion. Let's check
                        # if the resulting file is large enough so that
                        # we can accept the conversion anyway.
                        fs.stat target, (err, stats) ->
                                if err
                                        callback "Failed file conversion. No file exists."
                                        return
                                if stats.size >= FAILED_CONVERSION_IS_OK_SIZE
                                        @_audioGain target, callback
                                        return
                                callback "Failed file conversion. Not enough data converted (#{stats.size} bytes)."
                        return
                @_audioGain target, callback


class VorbisConverter extends AudioConverter
        suffix: =>
                return "ogg"

        audioType: =>
                return "audio/ogg"

        mimeType: =>
                return "application/ogg"

        _audioGain: (target, callback) =>
                options =
                        cwd: "/tmp/"
                vorbisgain = child_process.spawn "vorbisgain", [target], options
                vorbisgain.on 'exit', (code) =>
                        callback null

        convert: (source, bitrate, target, callback) =>
                options =
                        cwd: "/tmp/"
                ffmpeg = child_process.spawn "ffmpeg", ["-i", source, '-vn', '-acodec', 'libvorbis', '-ab', bitrate, '-ar', '48000', '-ac', '2', '-loglevel', 'quiet', '-y', target], options
                ffmpeg.on 'exit', (code) =>
                        @_conversionDone target, code, callback


class Mp3Converter extends AudioConverter
        suffix:  =>
                return "mp3"

        audioType: =>
                return "audio/mpeg"

        mimeType: =>
                return "audio/mpeg"

        _audioGain: (target, callback) =>
                options =
                        cwd: "/tmp/"
                mp3gain = child_process.spawn "mp3gain", [target], options
                mp3gain.on 'exit', (code) =>
                        callback null

        convert: (source, bitrate, target, callback) =>
                options =
                        cwd: "/tmp/"
                ffmpeg = child_process.spawn "ffmpeg", ["-i", source, '-vn', '-acodec', 'libmp3lame', '-ab', bitrate, '-y', '-ar', '48000', '-ac', '2', '-loglevel', 'quiet', target], options
                ffmpeg.on 'exit', (code) =>
                        @_conversionDone target, code, callback


class FileCacheInstance
        constructor: (@cache, @conversionParams) ->

        createCache: (convertedFilename, callback) =>
                return @cache.createCache convertedFilename, @conversionParams, callback

        getCached: (callback) =>
                return @cache.getCached @conversionParams, callback

        getKey: =>
                return @cache.getDigest @conversionParams


exports.FileCache = class FileCache
        constructor: (@cachedir="/tmp", @cacheLocation) ->

        generate: (conversionParams) =>
                return new FileCacheInstance @, conversionParams
<commit_msg>Opus encoder support class that is not in use.<commit_after>child_process = require 'child_process'
crypto = require 'crypto'
fs = require 'fs'
path = require 'path'
temp = require 'temp'

# 1 minute of 160 kbps music.
FAILED_CONVERSION_IS_OK_SIZE = 60 * 160 * 1000 / 8

FFMPEG = "ffmpeg"

class AudioConverter
        _conversionDone: (target, code, callback) =>
                if code != 0
                        # We have failed file conversion. Let's check
                        # if the resulting file is large enough so that
                        # we can accept the conversion anyway.
                        fs.stat target, (err, stats) ->
                                if err
                                        callback "Failed file conversion. No file exists."
                                        return
                                if stats.size >= FAILED_CONVERSION_IS_OK_SIZE
                                        @_audioGain target, callback
                                        return
                                callback "Failed file conversion. Not enough data converted (#{stats.size} bytes)."
                        return
                @_audioGain target, callback


class OpusConverter extends AudioConverter
        suffix: =>
                return "opus"

        audioType: =>
                return "audio/opus"

        mimeType: =>
                return "audio/ogg"

        _audioGain: (target, callback) =>
                callback null

        convert: (source, bitrate, target, callback) =>
                options =
                        cwd: "/tmp/"
                ffmpeg = child_process.spawn FFMPEG, ["-i", source, '-vn', '-acodec', 'libopus', '-ab', bitrate, '-ar', '48000', '-ac', '2', '-loglevel', 'quiet', '-y', target], options
                ffmpeg.on 'exit', (code) =>
                        @_conversionDone target, code, callback


class VorbisConverter extends AudioConverter
        suffix: =>
                return "ogg"

        audioType: =>
                return "audio/ogg"

        mimeType: =>
                return "application/ogg"

        _audioGain: (target, callback) =>
                options =
                        cwd: "/tmp/"
                vorbisgain = child_process.spawn "vorbisgain", [target], options
                vorbisgain.on 'exit', (code) =>
                        callback null

        convert: (source, bitrate, target, callback) =>
                options =
                        cwd: "/tmp/"
                ffmpeg = child_process.spawn FFMPEG, ["-i", source, '-vn', '-acodec', 'libvorbis', '-ab', bitrate, '-ar', '48000', '-ac', '2', '-loglevel', 'quiet', '-y', target], options
                ffmpeg.on 'exit', (code) =>
                        @_conversionDone target, code, callback


class Mp3Converter extends AudioConverter
        suffix:  =>
                return "mp3"

        audioType: =>
                return "audio/mpeg"

        mimeType: =>
                return "audio/mpeg"

        _audioGain: (target, callback) =>
                options =
                        cwd: "/tmp/"
                mp3gain = child_process.spawn "mp3gain", [target], options
                mp3gain.on 'exit', (code) =>
                        callback null

        convert: (source, bitrate, target, callback) =>
                options =
                        cwd: "/tmp/"
                ffmpeg = child_process.spawn FFMPEG, ["-i", source, '-vn', '-acodec', 'libmp3lame', '-ab', bitrate, '-y', '-ar', '48000', '-ac', '2', '-loglevel', 'quiet', target], options
                ffmpeg.on 'exit', (code) =>
                        @_conversionDone target, code, callback


class FileCacheInstance
        constructor: (@cache, @conversionParams) ->

        createCache: (convertedFilename, callback) =>
                return @cache.createCache convertedFilename, @conversionParams, callback

        getCached: (callback) =>
                return @cache.getCached @conversionParams, callback

        getKey: =>
                return @cache.getDigest @conversionParams


exports.FileCache = class FileCache
        constructor: (@cachedir="/tmp", @cacheLocation) ->

        generate: (conversionParams) =>
                return new FileCacheInstance @, conversionParams

{'fraction_non_alphanumeric': 0.10653138871274571, 'fraction_numerical': 0.009131261889663919, 'mean_word_length': 1.4445133292002479, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1318243', 'n_tokens_mistral': 2085, 'n_tokens_neox': 1885, 'n_words': 568}