How to read audio files

I wanna read an audio file
in python i would go about it like this :

import soundfile as sf 
def read_sound(file_name: str):

    if file_name.endswith(".flac"):

      with open(file_name, "rb") as f:

          speech, sample_rate = sf.read(f)

    elif file_name.endswith(".wav"):

      speech, sample_rate = sf.read(file_name)

    print(speech[:10])

just ignore the other code , I just want to be able to get the output like this :

image

Java provides API for reading sound files, an official guide is here: Java Sound Programmer Guide

1 Like

how would i import this in scala

You can read any file in Scala:
val myAudio = scala.io.Source.fromFile(“file.wav”)

What you want to do with the file? Stream audio over Internet, store in a database, convert into different format, indeed play it via Bluetooth using AAC codec? Your question is unclear.

1 Like

I just wanna be able to read it’s data in the format i showed above idk much about sound files so idk what it’s called but i wanna be ablel to read the data like i am doing above in python then i will normalize using some scala code i wrote and then i will pass it to a model then i will tokenize and finally decode it i prolly messsed up the sequence tho

https://pysoundfile.readthedocs.io/en/latest/#module-soundfile

Then you need to study formats of audio files and decode stream of raw bytes into stream of Floats accordingly. You can also use rich Java library recommended by @jpsacha, you can import it in Scala using “import” statement.
Good luck!