|
| 1 | +/* |
| 2 | + * Copyright 2024 k7t3 |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.github.k7t3.voicepeakcw4j.speech; |
| 18 | + |
| 19 | +import javax.sound.sampled.*; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Objects; |
| 23 | + |
| 24 | +/** |
| 25 | + * VOICEPEAKで生成したオーディオファイルを再生できるデバイス |
| 26 | + */ |
| 27 | +public class AudioDevice { |
| 28 | + |
| 29 | + /** |
| 30 | + * VOICEPEAKのコマンドライン実行で生成されるWAVEフォーマット |
| 31 | + */ |
| 32 | + private static final AudioFormat VOICEPEAK_DEFAULT_FORMAT = new AudioFormat( |
| 33 | + AudioFormat.Encoding.PCM_SIGNED, |
| 34 | + 48000, |
| 35 | + 16, |
| 36 | + 1, |
| 37 | + 2, |
| 38 | + 48000, |
| 39 | + false |
| 40 | + ); |
| 41 | + |
| 42 | + private final Mixer mixer; |
| 43 | + |
| 44 | + private AudioDevice(Mixer mixer) { |
| 45 | + this.mixer = mixer; |
| 46 | + } |
| 47 | + |
| 48 | + SourceDataLine getSourceDataLine() { |
| 49 | + var info = new DataLine.Info(SourceDataLine.class, VOICEPEAK_DEFAULT_FORMAT); |
| 50 | + try { |
| 51 | + return (SourceDataLine) mixer.getLine(info); |
| 52 | + } catch (LineUnavailableException e) { |
| 53 | + throw new RuntimeException(e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * 既定のデバイスを返す |
| 59 | + * <p> |
| 60 | + * 既定のデバイスが読み上げ音声を再生できないときはnullを返す |
| 61 | + * </p> |
| 62 | + * @return 既定のデバイス、あるいはそのデバイスが再生できないときはnull |
| 63 | + */ |
| 64 | + public static AudioDevice getDefaultDevice() { |
| 65 | + try { |
| 66 | + var sourceDataLineInfo = new DataLine.Info(SourceDataLine.class, VOICEPEAK_DEFAULT_FORMAT); |
| 67 | + var mixer = AudioSystem.getMixer(null); |
| 68 | + if (mixer.isLineSupported(sourceDataLineInfo)) { |
| 69 | + return new AudioDevice(mixer); |
| 70 | + } |
| 71 | + } catch (IllegalArgumentException ignored) { |
| 72 | + // 対応するミキサーがないときにIllegalArgumentExceptionがスローされるため無視する |
| 73 | + return null; |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * 読み上げ音声を再生可能なデバイスのリストを返す |
| 80 | + * @return 読み上げ音声を再生可能なデバイスのリスト |
| 81 | + */ |
| 82 | + public static List<AudioDevice> getAvailableDevices() { |
| 83 | + var sourceDataLineInfo = new DataLine.Info(SourceDataLine.class, VOICEPEAK_DEFAULT_FORMAT); |
| 84 | + return Arrays.stream(AudioSystem.getMixerInfo()) |
| 85 | + .map(AudioSystem::getMixer) |
| 86 | + .filter(m -> m.isLineSupported(sourceDataLineInfo)) |
| 87 | + .map(AudioDevice::new) |
| 88 | + .toList(); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean equals(Object o) { |
| 93 | + if (this == o) return true; |
| 94 | + if (!(o instanceof AudioDevice that)) return false; |
| 95 | + var info = mixer.getMixerInfo(); |
| 96 | + var oi = that.mixer.getMixerInfo(); |
| 97 | + return Objects.equals(info.getName(), oi.getName()) |
| 98 | + && Objects.equals(info.getDescription(), oi.getDescription()) |
| 99 | + && Objects.equals(info.getVendor(), oi.getVendor()) |
| 100 | + && Objects.equals(info.getVersion(), oi.getVersion()); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public int hashCode() { |
| 105 | + return Objects.hashCode(mixer); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String toString() { |
| 110 | + return mixer.getMixerInfo().toString(); |
| 111 | + } |
| 112 | +} |
0 commit comments