예제 #1
0
  public ADecoder() {
    info = new Info();
    info.init();

    comment = new Comment();
    comment.init();

    dsp = new DspState();
    block = new Block(dsp);
  }
예제 #2
0
 private void headerOut() throws IOException {
   synchronized (this) {
     if (vorbisInfo == null) {
       vorbisInfo = new Info();
       vorbisInfo.init();
       vorbisComment = new Comment();
       vorbisComment.init();
     }
   }
   if (vorbisInfo.synthesis_headerin(vorbisComment, oggPacket) < 0) {
     throw new IOException("error reading vorbis header");
   }
 }
 public int fetchHeaders(final Info info, final Comment comment, final int[] serialno, Page page) {
   if (page == null) {
     page = new Page();
     final int retValue = this.getNextPage(page, this.m_chunkSize);
     if (retValue == -128) {
       return -128;
     }
     if (retValue < 0) {
       return -130;
     }
   }
   if (serialno != null) {
     serialno[0] = page.serialno();
   }
   this.m_oggStreamState.init(page.serialno());
   info.init();
   comment.init();
   final Packet packet = new Packet();
   int i = 0;
   while (i < 3) {
     this.m_oggStreamState.pagein(page);
     while (i < 3) {
       final int result = this.m_oggStreamState.packetout(packet);
       if (result == 0) {
         break;
       }
       if (result == -1) {
         info.clear();
         this.m_oggStreamState.clear();
         return -1;
       }
       if (info.synthesis_headerin(comment, packet) != 0) {
         info.clear();
         this.m_oggStreamState.clear();
         return -1;
       }
       ++i;
     }
     if (i < 3 && this.getNextPage(page, 1L) < 0) {
       info.clear();
       this.m_oggStreamState.clear();
       return -1;
     }
   }
   return 0;
 }
예제 #4
0
  public void decode(Packet packet) {
    if (packetIndex < 3) {
      if (info.synthesis_headerin(comment, packet) < 0) {
        // error case; not a Vorbis header
        System.err.println("does not contain Vorbis audio data.");
        return;
      }
      if (packetIndex == 2) {
        dsp.synthesis_init(info);
        block.init(dsp);
        System.out.println("vorbis: " + info);
        System.out.println(comment.toString());
        index = new int[info.channels];

        if (stream == null) {
          stream = new AudioStream();
          stream.setupFormat(info.channels, 16, info.rate);
          stream.updateData(this, -1);
        }

        if (masterClock instanceof SystemClock) {
          SystemClock clock = (SystemClock) masterClock;
          if (clock.needReset()) {
            clock.reset();
            System.out.println("Note: master clock was reset by audio");
          }
        }
      }
    } else {
      long gp = packet.granulepos;
      if (gp != -1) {
        lastPts = (gp * Clock.SECONDS_TO_NANOS) / info.rate;
        lastWritten = ringBuffer.getTotalWritten();
        lastRead = ringBuffer.getTotalRead();
        lastPtsWrite = System.nanoTime();
      }

      decodeDsp(packet);
    }
    packetIndex++;
  }
예제 #5
0
  /*
   * Taken from the JOrbis Player
   */
  private void playStream(Thread me) throws InternalException {
    boolean chained = false;

    initJOrbis();

    while (true) {
      if (checkState()) {
        return;
      }

      int eos = 0;

      int index = oy.buffer(BUFSIZE);
      buffer = oy.data;
      try {
        bytes = bitStream.read(buffer, index, BUFSIZE);
      } catch (Exception e) {
        throw new InternalException(e);
      }
      oy.wrote(bytes);

      if (chained) {
        chained = false;
      } else {
        if (oy.pageout(og) != 1) {
          if (bytes < BUFSIZE) break;
          throw new InternalException("Input does not appear to be an Ogg bitstream.");
        }
      }
      os.init(og.serialno());
      os.reset();

      vi.init();
      vc.init();

      if (os.pagein(og) < 0) {
        // error; stream version mismatch perhaps
        throw new InternalException("Error reading first page of Ogg bitstream data.");
      }

      if (os.packetout(op) != 1) {
        // no page? must not be vorbis
        throw new InternalException("Error reading initial header packet.");
      }

      if (vi.synthesis_headerin(vc, op) < 0) {
        // error case; not a vorbis header
        throw new InternalException("This Ogg bitstream does not contain Vorbis audio data.");
      }

      int i = 0;

      while (i < 2) {
        while (i < 2) {
          if (checkState()) {
            return;
          }

          int result = oy.pageout(og);
          if (result == 0) break; // Need more data
          if (result == 1) {
            os.pagein(og);
            while (i < 2) {
              result = os.packetout(op);
              if (result == 0) break;
              if (result == -1) {
                throw new InternalException("Corrupt secondary header.  Exiting.");
              }
              vi.synthesis_headerin(vc, op);
              i++;
            }
          }
        }

        index = oy.buffer(BUFSIZE);
        buffer = oy.data;
        try {
          bytes = bitStream.read(buffer, index, BUFSIZE);
        } catch (Exception e) {
          throw new InternalException(e);
        }
        if (bytes == 0 && i < 2) {
          throw new InternalException("End of file before finding all Vorbis headers!");
        }
        oy.wrote(bytes);
      }

      convsize = BUFSIZE / vi.channels;

      vd.synthesis_init(vi);
      vb.init(vd);

      float[][][] _pcmf = new float[1][][];
      int[] _index = new int[vi.channels];

      getOutputLine(vi.channels, vi.rate);

      while (eos == 0) {
        while (eos == 0) {
          if (player != me) {
            return;
          }

          int result = oy.pageout(og);
          if (result == 0) break; // need more data
          if (result == -1) { // missing or corrupt data at this page
            // position
            // System.err.println("Corrupt or missing data in
            // bitstream;
            // continuing...");
          } else {
            os.pagein(og);

            if (og.granulepos() == 0) { //
              chained = true; //
              eos = 1; //
              break; //
            } //

            while (true) {
              if (checkState()) {
                return;
              }

              result = os.packetout(op);
              if (result == 0) break; // need more data
              if (result == -1) { // missing or corrupt data at
                // this page position
                // no reason to complain; already complained
                // above

                // System.err.println("no reason to complain;
                // already complained above");
              } else {
                // we have a packet. Decode it
                int samples;
                if (vb.synthesis(op) == 0) { // test for
                  // success!
                  vd.synthesis_blockin(vb);
                }
                while ((samples = vd.synthesis_pcmout(_pcmf, _index)) > 0) {
                  if (checkState()) {
                    return;
                  }

                  float[][] pcmf = _pcmf[0];
                  int bout = (samples < convsize ? samples : convsize);

                  // convert doubles to 16 bit signed ints
                  // (host order) and
                  // interleave
                  for (i = 0; i < vi.channels; i++) {
                    int ptr = i * 2;
                    // int ptr=i;
                    int mono = _index[i];
                    for (int j = 0; j < bout; j++) {
                      int val = (int) (pcmf[i][mono + j] * 32767.);
                      if (val > 32767) {
                        val = 32767;
                      }
                      if (val < -32768) {
                        val = -32768;
                      }
                      if (val < 0) val = val | 0x8000;
                      convbuffer[ptr] = (byte) (val);
                      convbuffer[ptr + 1] = (byte) (val >>> 8);
                      ptr += 2 * (vi.channels);
                    }
                  }
                  outputLine.write(convbuffer, 0, 2 * vi.channels * bout);
                  vd.synthesis_read(bout);
                }
              }
            }
            if (og.eos() != 0) eos = 1;
          }
        }

        if (eos == 0) {
          index = oy.buffer(BUFSIZE);
          buffer = oy.data;
          try {
            bytes = bitStream.read(buffer, index, BUFSIZE);
          } catch (Exception e) {
            throw new InternalException(e);
          }
          if (bytes == -1) {
            break;
          }
          oy.wrote(bytes);
          if (bytes == 0) eos = 1;
        }
      }

      os.clear();
      vb.clear();
      vd.clear();
      vi.clear();
    }

    oy.clear();
  }
 /** Reads headers and comments. */
 private void readHeaders(HashMap aff_properties, HashMap af_properties) throws IOException {
   if (TDebug.TraceAudioConverter) TDebug.out("readHeaders(");
   index = oggSyncState_.buffer(bufferSize_);
   buffer = oggSyncState_.data;
   bytes = readFromStream(buffer, index, bufferSize_);
   if (bytes == -1) {
     if (TDebug.TraceAudioConverter)
       TDebug.out("Cannot get any data from selected Ogg bitstream.");
     throw new IOException("Cannot get any data from selected Ogg bitstream.");
   }
   oggSyncState_.wrote(bytes);
   if (oggSyncState_.pageout(oggPage_) != 1) {
     if (bytes < bufferSize_) {
       throw new IOException("EOF");
     }
     if (TDebug.TraceAudioConverter) TDebug.out("Input does not appear to be an Ogg bitstream.");
     throw new IOException("Input does not appear to be an Ogg bitstream.");
   }
   oggStreamState_.init(oggPage_.serialno());
   vorbisInfo.init();
   vorbisComment.init();
   aff_properties.put("ogg.serial", new Integer(oggPage_.serialno()));
   if (oggStreamState_.pagein(oggPage_) < 0) {
     // error; stream version mismatch perhaps
     if (TDebug.TraceAudioConverter) TDebug.out("Error reading first page of Ogg bitstream data.");
     throw new IOException("Error reading first page of Ogg bitstream data.");
   }
   if (oggStreamState_.packetout(oggPacket_) != 1) {
     // no page? must not be vorbis
     if (TDebug.TraceAudioConverter) TDebug.out("Error reading initial header packet.");
     throw new IOException("Error reading initial header packet.");
   }
   if (vorbisInfo.synthesis_headerin(vorbisComment, oggPacket_) < 0) {
     // error case; not a vorbis header
     if (TDebug.TraceAudioConverter)
       TDebug.out("This Ogg bitstream does not contain Vorbis audio data.");
     throw new IOException("This Ogg bitstream does not contain Vorbis audio data.");
   }
   int i = 0;
   while (i < 2) {
     while (i < 2) {
       int result = oggSyncState_.pageout(oggPage_);
       if (result == 0) {
         break;
       } // Need more data
       if (result == 1) {
         oggStreamState_.pagein(oggPage_);
         while (i < 2) {
           result = oggStreamState_.packetout(oggPacket_);
           if (result == 0) {
             break;
           }
           if (result == -1) {
             if (TDebug.TraceAudioConverter) TDebug.out("Corrupt secondary header.  Exiting.");
             throw new IOException("Corrupt secondary header.  Exiting.");
           }
           vorbisInfo.synthesis_headerin(vorbisComment, oggPacket_);
           i++;
         }
       }
     }
     index = oggSyncState_.buffer(bufferSize_);
     buffer = oggSyncState_.data;
     bytes = readFromStream(buffer, index, bufferSize_);
     if (bytes == -1) {
       break;
     }
     if (bytes == 0 && i < 2) {
       if (TDebug.TraceAudioConverter)
         TDebug.out("End of file before finding all Vorbis headers!");
       throw new IOException("End of file before finding all Vorbis  headers!");
     }
     oggSyncState_.wrote(bytes);
   }
   // Read Ogg Vorbis comments.
   byte[][] ptr = vorbisComment.user_comments;
   String currComment = "";
   int c = 0;
   for (int j = 0; j < ptr.length; j++) {
     if (ptr[j] == null) {
       break;
     }
     currComment = (new String(ptr[j], 0, ptr[j].length - 1, "UTF-8")).trim();
     if (TDebug.TraceAudioConverter) TDebug.out(currComment);
     if (currComment.toLowerCase().startsWith("artist")) {
       aff_properties.put("author", currComment.substring(7));
     } else if (currComment.toLowerCase().startsWith("title")) {
       aff_properties.put("title", currComment.substring(6));
     } else if (currComment.toLowerCase().startsWith("album")) {
       aff_properties.put("album", currComment.substring(6));
     } else if (currComment.toLowerCase().startsWith("date")) {
       aff_properties.put("date", currComment.substring(5));
     } else if (currComment.toLowerCase().startsWith("copyright")) {
       aff_properties.put("copyright", currComment.substring(10));
     } else if (currComment.toLowerCase().startsWith("comment")) {
       aff_properties.put("comment", currComment.substring(8));
     } else if (currComment.toLowerCase().startsWith("genre")) {
       aff_properties.put("ogg.comment.genre", currComment.substring(6));
     } else if (currComment.toLowerCase().startsWith("tracknumber")) {
       aff_properties.put("ogg.comment.track", currComment.substring(12));
     } else {
       c++;
       aff_properties.put("ogg.comment.ext." + c, currComment);
     }
     aff_properties.put(
         "ogg.comment.encodedby",
         new String(vorbisComment.vendor, 0, vorbisComment.vendor.length - 1));
   }
 }