0

Heloo! I was trying to read bytes per second from an input audio wav files and write those bytes data per second by skipping some bytes to a new audio wav file. My question is how to skip some bytes per second from the input file and writing only remaining bytes data per second to new audio file in java programming. I have code it as:- p is an array which I am using to classify audio file seconds.508 is the file size of input audio file. Means if at 10th second of audio p[q]==0, then the code should not read bytes at 10th seconds. Please highlight where I am doing wrong in this code? The code is running fine, and generating an audio file with size less than the input audio, but it is not playing, neither I can see its duration.

FileInputStream in=new FileInputStream...
FileOutputStream out=new FileOutputStream...
int q=0;
byte[] buf = new byte[44100];
int len;
while (((len = in.read(buf)) > 0)&&(q<508)) {
    if(p[q]==1){
        out.write(buf, 0, len);
     }
    q++;
}
in.close();
out.close();
alicefrozen
  • 39
  • 1
  • 5
  • You are reading raw bytes, so if p[0] == 0 then you are not copying the WAV header. Try first copying the first 44 (or 46) bytes of the file, then treating the remainder as audio. See https://stackoverflow.com/questions/19991405/how-can-i-detect-whether-a-wav-file-has-a-44-or-46-byte-header – Conrad Parker Sep 11 '17 at 01:09
  • if you are reading a WAV file you'll need to parse it properly (or use a library which does this) to extract audio "frames" and track their size. you can't just skip bytes "randomly" in a WAV file as it will corrupt the stream. I have had to solve this issue recently for an AAC shoutcast server i wrote. I have to read markers in the AAC stream to detect the start of each frame. Its going to be different for WAV of course. – slipperyseal Sep 11 '17 at 01:10
  • oops. maybe after the header of a WAV file the data is raw and has no frames. i'll leave that to people who know :) – slipperyseal Sep 11 '17 at 01:12
  • @slipperyseal I have also tried the same task using jaudio library, and working on AudioInputStream. I am new to this field, can you provide me code of AAC, I know how to read bytes from audio wav file, but don't know how to write. need help me in that. – alicefrozen Sep 11 '17 at 01:14
  • Either way, it's better to use a library. If the audio format is anything other than 8bit mono, there are more than 44100 bytes per second, and some WAV files have other sections etc. – Conrad Parker Sep 11 '17 at 01:14
  • @alicefrozen have a look at this, but i think it wont be of use to you even if you use AAC. each frame is encoded and i haven't parsed the time information from it yet, so it would have less information than simply reading n bytes from a WAV file. https://github.com/slipperyseal/sealcast/ – slipperyseal Sep 11 '17 at 01:17
  • nad how to provide header to wav? – alicefrozen Sep 11 '17 at 01:17
  • @ConradParker can you provide me code? I have tried alot and searched alot, but couldn't generate the audio file. – alicefrozen Sep 11 '17 at 01:23
  • Perhaps try some of the code and links of https://stackoverflow.com/questions/3297749/java-reading-manipulating-and-writing-wav-files – Conrad Parker Sep 11 '17 at 01:28
  • They these are not helping. – alicefrozen Sep 11 '17 at 14:12
  • @ConradParker how to read bytes per seconds ,of only some seconds in a video file, I am stuck in this actually. Means is there any way to read only some random seconds (which we want) from a video? – alicefrozen Sep 14 '17 at 04:34
  • A video file as well? you really need to use some multimedia libraries. it's not possible to just read some number of bytes: modern video files have complex structure, and you need to decode keyframes etc. Watch my linux.conf.au 2009 presentation on exactly this topic of extracting seconds from a video file: https://www.youtube.com/watch?v=nLJNoQCR6ss – Conrad Parker Sep 14 '17 at 04:47
  • @ConradParker sorry That was my mistake, I wanted to say from an audio file? – alicefrozen Sep 14 '17 at 04:59
  • @ConradParker I want to extract different seconds from an audio file and then concatenate them and generate to a new wav file? – alicefrozen Sep 14 '17 at 05:04
  • @alicefrozen the general advice is that you should use a library. I can't recommend a Java library (I'd use libsndfile in C/C++). Try one of the suggestions above, and if you get stuck then please ask a new question, including your new attempt at coding this up. Simply saying "Those are not helping" doesn't give anyone any clues about what problems you are having. – Conrad Parker Sep 15 '17 at 03:35
  • I have already tried this, then said to you. – alicefrozen Sep 16 '17 at 01:38
  • @ConradParker reply please?? – alicefrozen Sep 19 '17 at 23:30
  • All you have said regarding your attempts to use any media file library is "They these are not helping". I'm sorry but it's not really possible to help you any further unless you actually try something and show what you have tried. When you do that, please start a new question. – Conrad Parker Sep 19 '17 at 23:34

0 Answers0