How can i reload a wav file using openAL (java) - audio

I made up an application which loads a .wav file, but it loads it only once.
I want to be able to reload a wav file after the first track ends.How can i make that in java?
Also, if i hit play twice after the end of the track(while the selected track is loaded) it throws an
IllegalThreadStateException.
if (threadStatus != 0 || this != me)
throw new IllegalThreadStateException();
the code i use to load the file is this:
OpenAL openal = new OpenAL();
source = openal.createSource(new File(Audioplayer.path));
System.out.println(source.toString());
source.play();
source.setGain(0.75f); // 75% volume
source.setPitch(0.85f); // 85% of the original pitch
source.setPosition(0, 0, 0); // -1 means 1 unit to the left
source.setLooping(false); // Loop the sound effect
j=source.getBuffer();
System.out.println(j);
for (i=1;i<=10000;i++){
Thread.sleep(1); // Wait for 10 seconds
}
Thread.sleep(10000); // Wait for 10 seconds
source.close();
openal.close();

Just surround the area with a loop and it will reload that bit of code every time it is done.

Related

Sphinx Voice Activity Detection

So I'm trying to write a simple program that will detect voice activity with a .wav file using the CMU Sphinx library.
So far, I have the following
SpeechClassifier s = new SpeechClassifier();
s.setPredecessor(dataSource);
Data d = s.getData();
while(d != null) {
if(s.isSpeech()) {
System.out.println("Speech is detected");
}
else {
System.out.println("Speech has not been detected");
}
System.out.println();
d = s.getData();
}
I get the output "Speech is not detected" but there is Speech in the audio file. It seems as if the getData function is not working the way I want it to. I want it to get the frames and then determine whether the frames (s.isSpeech()) contain speech or not.
I'm trying to have multiple outputs ("Speech is detected" vs "Speech is not detected") for each frame. How can I make my code better? Thanks!
You need to insert DataBlocker before SpeechClassifier:
DataBlocker b = new DataBlocker(10); // means 10ms
SpeechClassifier s = new SpeechClassifier(10, 0.003, 10, 0);
b.setPredecessor(dataSource);
s.setPredecessor(b);
Then it will process 10 millisecond frames.

Raw audio playback in Allegro 5

I'm writing a MOD player, trying to playback a sample using Allegro5 raw stream capabilities, I can't figure out the exact init parameters for the stream to play the loaded sample data from the mod file.
This is what I have:
xf::ModLoader ml;
ml.loadFromFile("C:\\Users\\bubu\\Downloads\\agress.mod");
// getSampleLength() returns # of data words
int sample_length = ml.getSampleLength(1) * 2;
const int8_t* sample_data = ml.getSampleData(1);
ALLEGRO_MIXER* mixer = al_get_default_mixer();
ALLEGRO_AUDIO_STREAM* stream = al_create_audio_stream(1, sample_length, 8363, ALLEGRO_AUDIO_DEPTH_INT8, ALLEGRO_CHANNEL_CONF_1);
al_attach_audio_stream_to_mixer(stream, mixer);
al_set_audio_stream_gain(stream, 0.7f);
al_set_audio_stream_playmode(stream, ALLEGRO_PLAYMODE_ONCE);
al_set_audio_stream_playing(stream, true);
al_set_audio_stream_fragment(stream, (void*)sample_data);
al_drain_audio_stream(stream);
First of all, freq param is hardcoded for the test (8363Hz), but, playing back at the specified frequency I don't get what I expect, and al_drain_audio_stream() gets stuck forever playing garbage in a loop...
Any help would be appreciated.
At the very least, you need to be calling al_get_audio_stream_fragment before you call al_set_audio_stream_fragment. Typically you'd feed these streams in a while loop, while responding to the ALLEGRO_EVENT_AUDIO_STREAM_FRAGMENT event. See the ex_saw example in the Allegro's source for some sample code: https://github.com/liballeg/allegro5/blob/master/examples/ex_saw.c

Unwanted audio delay in Corona SDK

I am currently making an extremely simple app where when an image is tapped, a sound plays and some text records the amount of clicks. However, I noticed some significant delay in the audio, where it would take half a second for the sound to play after the image is clicked. Does anyone have any ideas on why this might be the case?
local function btnTouch(event)
if event.phase == "began" then
media.playSound( "btnSnd.mp3" )
score = score + 1
btnText.text = score
return true
end
end
--code
imageBtn:addEventListener("touch", btnTouch)
The answer is probably that the sound needs to be loaded. Try and switch it to audio instead and preload it. Try and see if this gives you the desired result:
local buttonSendAudio = audio.loadSound( "btnSnd.mp3")
local function btnTouch(event)
if event.phase == "began" then
audio.play( buttonSendAudio )
score = score + 1
btnText.text = score
return true
end
end
--code
imageBtn:addEventListener("touch", btnTouch)
https://docs.coronalabs.com/daily/guide/media/audioSystem/index.html
Worth checking if sound has some silence stored in mp3.
Open it with any audio editor, see sound's waveform.

JAudioTagger Deleting First Few Seconds of Track

I've written a simple Groovy script (below) to set the values of four of the ID3v1 and ID3v2 tag fields in mp3 files using the JAudioTagger library. The script successfully makes the changes but it also deletes the first 5 to 10 seconds of some of the files, other files are unaffected. It's not a big problem, but if anyone knows a simple fix, I would be grateful. All the files are from the same source, all have v1 and v2 tags, I can find no obvious difference in the source files to explain it.
import org.jaudiotagger.*
java.util.logging.Logger.getLogger("org.jaudiotagger").setLevel(java.util.logging.Level.OFF)
Integer trackNum = 0
Integer totalFiles = 0
Integer invalidFiles = 0
validMP3File = true
def dir = new File(/D:\Users\Jeremy\Music\Speech Radio\Unlistened\Z Temp Files to MP3 Tagged/)
dir.eachFile({curFile ->
totalFiles ++
try {
mp3File = org.jaudiotagger.audio.AudioFileIO.read(curFile)
} catch (org.jaudiotagger.audio.exceptions.CannotReadException e) {
validMP3File = false
invalidFiles ++
}
// Get the file name excluding the extension
baseFilename = org.jaudiotagger.audio.AudioFile.getBaseFilename(curFile)
// Check that it is an MP3 file
if (validMP3File) {
if (mp3File.getAudioHeader().getEncodingType() != 'mp3') {
validMP3File = false
invalidFiles ++
}
}
if (validMP3File) {
trackNum ++
if (mp3File.hasID3v1Tag()) {
curTagv1 = mp3File.getID3v1Tag()
} else {
curTagv1 = new org.jaudiotagger.tag.id3.ID3v1Tag()
}
if (mp3File.hasID3v2Tag()) {
curTagv2 = mp3File.getID3v2TagAsv24()
} else {
curTagv2 = new org.jaudiotagger.tag.id3.ID3v23Tag()
}
curTagv1.setField(org.jaudiotagger.tag.FieldKey.TITLE, baseFilename)
curTagv2.setField(org.jaudiotagger.tag.FieldKey.TITLE, baseFilename)
curTagv1.setField(org.jaudiotagger.tag.FieldKey.ARTIST, "BBC Radio")
curTagv2.setField(org.jaudiotagger.tag.FieldKey.ARTIST, "BBC Radio")
curTagv1.setField(org.jaudiotagger.tag.FieldKey.ALBUM, "BBC Radio - 20130205")
curTagv2.setField(org.jaudiotagger.tag.FieldKey.ALBUM, "BBC Radio - 20130205")
curTagv1.setField(org.jaudiotagger.tag.FieldKey.TRACK, trackNum.toString())
curTagv2.setField(org.jaudiotagger.tag.FieldKey.TRACK, trackNum.toString())
mp3File.setID3v1Tag(curTagv1)
mp3File.setID3v2Tag(curTagv2)
mp3File.save()
}
})
println """$trackNum tracks created from $totalFiles files with $invalidFiles invalid files"""
I'm still investigating and it appears that there is no problem with JAudioTagger. Before setting the tags, I use Total Recorder to reduce the quality of the download from 128kbps, 44,100Hz to 56kbps, 22,050Hz. This reduces the file size to less than half and the quality is fine for speech radio.
If I run my script on the original files, none of the audio track is deleted. The deletion of the first part of the audio track only occurs with the files that have been processed by Total Recorder.
Looking at the JAudioTagger logging for these files, there does appear to be a problem with the header:
Checking further because the ID3 Tag ends at 0x23f9 but the mp3 audio doesnt start until 0x7a77
Confirmed audio starts at 0x7a77 whether searching from start or from end of ID3 tag
This check is not performed for files that have not been processed by Total Recorder.
The log of the header read operation also shows (for a 27 minute track):
trackLength:06:52
It looks as though I shall have to find a new MP3 file editor!
Instead of
mp3File.save()
could you try:
mp3File.commit()
No idea if it will help, but that seems to be the documented method?

java me: start playing mp3 file from the 2nd minute and stop on the fourth minute. mp3 file is of 6 minutes

i need to start playing a local resource mp3 file from the 2nd minute and stop on the fourth minute. mp3 file is of 6 minutes.
im new to this and couldn't find an example for the below code, could some1 pls point me to something like below?
long setMediaTime(long now)
i have other files also which i want to do the same with different numbers, it would be best if i could do this in milliseconds.. i am using this code to play the file..
{
try
{
InputStream is = getClass().getResourceAsStream("/nn.mp3");
Player p = Manager.createPlayer(is,"audio/mpeg");
p.realize();
{
}
p.prefetch();
p.start();
}
catch(Exception e)
{}
}
thanking u in advance! :)
You can use this p.setLoopCount( ); method.
Just have a look here

Resources