📄 wavheader.java
字号:
output.append("\n================ rdata chunk ==================="); output.append("\n08-11 Letters : ").append(wID); output.append("\n=============== format chunk ================"); output.append("\n12-15 Letters : ").append(fID); output.append("\n16-19 Length of rest of chunk : ").append(fLen); output.append("\n20-21 WAV Format Tag : ").append(wFormatTag); output.append("\n22-23 Number of channels : ").append(nChannels); output.append("\n24-27 Sample frequency : ").append(nSamplesPerSec); output.append("\n28-31 nAvgBytesPerSec : ").append(nAvgBytesPerSec); output.append("\n32-33 nBlockAlign : ").append(nBlockAlign); for (int i = 0; i < formatSpecific.length; i++) { output.append("\n").append((34 + (i * 2)) + "-" + (35 + (i * 2))); output.append(" Format specific data : ").append(formatSpecific[i]); } output.append("\n================ data chunk ================="); output.append("\n" + (20 + fLen) + "-" + (23 + fLen)).append(" Letters : "); output.append(dID).append("\n" + (24 + fLen) + "-" + (27 + fLen)); output.append(" Length of following data: ") .append(dLen + "\n" + (28 + fLen) + "-" + (28 + fLen + dLen)) .append(" (data)"); if (cuePoints.length > 0) { output.append("\n================= cue Chunk ================="); for (int i = 0; i < cuePoints.length; i++) { output.append("\nCue point " + i + ":\n" + cuePoints[i]); } } if (cueSections.length > 0) { output.append("\n==================list chunk ================"); output.append("\n============ labeled text chunk ==========="); for (int i = 0; i < cueSections.length; i++) { output.append("\nCue section " + i + ":\n" + cueSections[i]); } } output.append(getInfo()); } catch (NullPointerException e) { e.printStackTrace(); return ""; } return output.toString(); } private static WAVCuePoint[] getCuePoints(RandomAccessFile soundFile) throws IOException { byte[] b = new byte[4]; soundFile.read(b); //cueChunkDataSize soundFile.read(b); int numCuePoints = getInt(b); WAVCuePoint[] cuePoints = new WAVCuePoint[numCuePoints]; int ID; int position; String dataChunkID; int chunkStart; int blockStart; int sampleOffset; for (int i = 0; i < cuePoints.length; i++) { soundFile.read(b); ID = getInt(b); soundFile.read(b); position = getInt(b); soundFile.read(b); dataChunkID = getString(b); soundFile.read(b); chunkStart = getInt(b); soundFile.read(b); blockStart = getInt(b); soundFile.read(b); sampleOffset = getInt(b); if ("data".equals(dataChunkID)) { cuePoints[i] = new WAVCuePoint(ID, position, chunkStart, blockStart, sampleOffset); } else { System.out.println("Warning: Reading of cue points failed!"); System.out.println( "Cannot handle Cue Point with Data Chunk ID '" + dataChunkID + "'"); return new WAVCuePoint[0]; } } return cuePoints; } private static WAVCueSection getCueSection(RandomAccessFile soundFile, WAVCuePoint[] cuePoints) throws IOException { byte[] b = new byte[4]; byte[] s = new byte[2]; byte[] t; WAVCueSection cueSection = null; int cuePointID; int sampleLength; String purposeID; short country; short language; short dialect; short codePage; String label; long seek = soundFile.getFilePointer(); soundFile.read(b); int chunkDataSize = getInt(b); soundFile.read(b); cuePointID = getInt(b); soundFile.read(b); sampleLength = getInt(b); soundFile.read(b); purposeID = getString(b); soundFile.read(s); country = getShort(s); soundFile.read(s); language = getShort(s); soundFile.read(s); dialect = getShort(s); soundFile.read(s); codePage = getShort(s); if ((chunkDataSize - 20 - 1) >= 0) { t = new byte[chunkDataSize - 20 - 1]; // minus cuePoint & language bytes minus string end char soundFile.read(t); label = getString(t); } else { label = ""; } for (int i = 0; i < cuePoints.length; i++) { if (cuePoints[i].getID() == cuePointID) { cueSection = new WAVCueSection(cuePoints[i], sampleLength, purposeID, country, language, dialect, codePage, label); break; } } seek += (chunkDataSize + 4); seek += (seek % 2); //add 1 if uneven soundFile.seek(seek); return cueSection; } private static String getInfo(RandomAccessFile soundFile) throws IOException { String info = ""; byte[] b = new byte[4]; soundFile.read(b); int chunkDataSize = getInt(b); if (chunkDataSize > 0) { byte[] t = new byte[chunkDataSize]; soundFile.read(t); info = getString(t); } soundFile.seek(soundFile.getFilePointer() + (soundFile.getFilePointer() % 2)); return info; } private static int getInt(byte[] bytes) { return getInt(bytes[0], bytes[1], bytes[2], bytes[3]); } private static int getInt(byte b1, byte b2, byte b3, byte b4) { return (b1 & 0xff) | ((b2 & 0xff) << 8) | ((b3 & 0xff) << 16) | ((b4 & 0xff) << 24); } private static short getShort(byte[] s) { return getShort(s[0], s[1]); } private static short getShort(byte b1, byte b2) { return (short) ((b1 & 0xff) | ((b2 & 0xff) << 8)); } private static String getString(byte[] bytes) { return getString(bytes, bytes.length); } private static String getString(byte[] bytes, int nrOfBytes) { char[] asChar = new char[nrOfBytes]; for (int i = 0; i < nrOfBytes; i++) { asChar[i] = (bytes[i] > 32) ? (char) bytes[i] : ' '; } return new String(asChar); } private void readAssociatedDataList(RandomAccessFile soundFile) { ArrayList cueSectionList = new ArrayList(); byte[] b = new byte[4]; try { while (soundFile.getFilePointer() < soundFile.length()) { soundFile.read(b); if ("ltxt".equals(getString(b))) { WAVCueSection cueSection = getCueSection(soundFile, cuePoints); if (cueSection != null) { cueSectionList.add(cueSection); } } else if ("labl".equals(getString(b))) { readCuePointLabels(soundFile, cuePoints); } else if ("note".equals(getString(b))) { readCuePointNotes(soundFile, cuePoints); } else { break; } } cueSections = (WAVCueSection[]) cueSectionList.toArray(new WAVCueSection[0]); } catch (IOException e) { System.out.println(e.getMessage()); } } private static void readCuePointLabels(RandomAccessFile soundFile, WAVCuePoint[] cuePoints) throws IOException { byte[] b = new byte[4]; byte[] t; String label; long seek = soundFile.getFilePointer(); soundFile.read(b); int chunkDataSize = getInt(b); soundFile.read(b); int cuePointID = getInt(b); if ((chunkDataSize - 4 - 1) >= 0) { t = new byte[chunkDataSize - 4 - 1]; // minus cuePoint bytes minus string end byte (&x00) soundFile.read(t); label = getString(t); for (int i = 0; i < cuePoints.length; i++) { if (cuePoints[i].getID() == cuePointID) { cuePoints[i].setLabel(label); break; } } } seek += (chunkDataSize + 4); seek += (seek % 2); //add 1 if uneven soundFile.seek(seek); } private static void readCuePointNotes(RandomAccessFile soundFile, WAVCuePoint[] cuePoints) throws IOException { byte[] b = new byte[4]; byte[] t; String label; long seek = soundFile.getFilePointer(); soundFile.read(b); int chunkDataSize = getInt(b); soundFile.read(b); int cuePointID = getInt(b); if ((chunkDataSize - 4 - 1) >= 0) { t = new byte[chunkDataSize - 4 - 1]; // minus cuePoint bytes minus string end byte (&x00) soundFile.read(t); label = getString(t); for (int i = 0; i < cuePoints.length; i++) { if (cuePoints[i].getID() == cuePointID) { cuePoints[i].setNote(label); break; } } } seek += (chunkDataSize + 4); seek += (seek % 2); //add 1 if uneven soundFile.seek(seek); } private void readCues(RandomAccessFile soundFile) { byte[] b = new byte[4]; try { soundFile.seek(28 + fLen + dLen); int listChunkSize = 0; while (soundFile.getFilePointer() < soundFile.length()) { soundFile.read(b); if ("list".equals(getString(b).toLowerCase())) { soundFile.read(b); listChunkSize = getInt(b); } if ("cue ".equals(getString(b))) { cuePoints = getCuePoints(soundFile); continue; } else if ("adtl".equals(getString(b))) { readAssociatedDataList(soundFile); continue; } else if ("info".equals(getString(b).toLowerCase())) { long endOfChunk = (soundFile.getFilePointer() + listChunkSize) - 4; while (soundFile.getFilePointer() < endOfChunk) { soundFile.read(b); infos.put(getString(b), getInfo(soundFile)); } } } } catch (IOException e) { System.out.println(e.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -