コード例 #1
0
  public static void main(String[] args) {
    if (args.length != 1) {
      Logger.println("Usage:  java Speaker <input file>");
      System.exit(1);
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    Speaker speaker = null;

    while (true) {
      try {
        speaker =
            new SpeakerJavasoundImpl(
                8000, 1, RtpPacket.getDataSize(RtpPacket.PCM_ENCODING, 8000, 1));
      } catch (Exception e) {
        Logger.println(e.getMessage());
        System.exit(1);
      }

      Logger.logLevel = 5;

      try {
        TreatmentManager t =
            new TreatmentManager(args[0], 0, speaker.getSampleRate(), speaker.getChannels());

        while (true) {
          byte[] audioData = t.getLinearDataBytes(RtpPacket.PACKET_PERIOD);

          if (audioData == null) {
            break; // end of file
          }

          speaker.write(audioData, 0, audioData.length);
        }
      } catch (IOException e) {
        Logger.println(e.getMessage());
      }

      if (speaker != null) {
        speaker.done();
      }

      Logger.println("Press return to play the file again...");

      try {
        String line = br.readLine();
        if (line.equalsIgnoreCase("quit")) {
          break;
        }
      } catch (IOException ioe) {
        break;
      }
    }
  }
コード例 #2
0
  public SpeakerJavasoundImpl(int sampleRate, int channels, int bufferSize) throws IOException {

    this.sampleRate = sampleRate;
    this.channels = channels;
    this.bufferSize = bufferSize;

    chunkSize = RtpPacket.getDataSize(RtpPacket.PCM_ENCODING, sampleRate, channels);

    try {
      setupSpeaker();
    } catch (LineUnavailableException e) {
      throw new IOException(e.getMessage());
    }

    if (Logger.logLevel >= Logger.LOG_INFO) {
      Logger.println("New speaker " + sampleRate + "/" + channels + " bufferSize " + bufferSize);
    }

    double volumeLevel = Utils.getDoublePreference(VOLUME_LEVEL);

    if (volumeLevel != -1D) {
      this.volumeLevel = volumeLevel;
    }
  }