<commit_before>package aural

import (
	"io"
	"log"
	"os"
	"path"

	"github.com/badgerodon/mp3"
	"github.com/mkb218/gosndfile/sndfile"
)

type AudioSourceFactory func() AudioSource

type AudioSource interface {
	ReadFrames(out []int32) (int64, error)

	Channels() int32
	SampleRate() int32

	Open(string) error
	Close()
}

var sourceTypes map[string]AudioSourceFactory

func init() {
	sourceTypes = make(map[string]AudioSourceFactory)
	sourceTypes[".mp3"] = NewMP3AudioSource
}

func NewAudioSource(identifier string) AudioSource {
	// TODO: Support URLs, not just file paths
	extension := path.Ext(identifier)
	factory, ok := sourceTypes[extension]

	if ok == false {
		factory = NewLibSndFileAudioSource
	}

	return factory()<commit_msg>Add filetype check using magic numbers<commit_after>package aural

import (
	"io"
	"io/ioutil"
	"log"
	"os"
	"path"

	"github.com/badgerodon/mp3"
	"github.com/mkb218/gosndfile/sndfile"

	"gopkg.in/h2non/filetype.v0"
)

type AudioSourceFactory func() AudioSource

type AudioSource interface {
	ReadFrames(out []int32) (int64, error)

	Channels() int32
	SampleRate() int32

	Open(string) error
	Close()
}

var sourceTypes map[string]AudioSourceFactory

func init() {
	sourceTypes = make(map[string]AudioSourceFactory)
	sourceTypes["mp3"] = NewMP3AudioSource
}

func GetExtensionFor(identifier string) string {
	buf, err := ioutil.ReadFile(identifier)
	extension := path.Ext(identifier)

	if kind, unknown := filetype.Match(buf); err == nil {
		if unknown == nil {
			extension = kind.Extension
		}
	}

	return extension
}

func NewAudioSource(identifier string) AudioSource {
	// TODO: Support URLs, not just file paths

	extension := GetExtensionFor(identifier)
	factory, ok := sourceTypes[extension]

	if ok != true {
		factory = NewLibSndFileAudioSource
	}

	return factory()
{'fraction_non_alphanumeric': 0.1140495867768595, 'fraction_numerical': 0.01652892561983471, 'mean_word_length': 5.013245033112582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4683370', 'n_tokens_mistral': 655, 'n_tokens_neox': 616, 'n_words': 146}