本帖最后由 —?—! 于 2022-12-20 14:06 编辑
如题,求助大佬。
我用其他录音软件录制都不会出现电流声,应该不是设备的问题。
主要代码如下(是一个录音并实时播放的代码):
复制代码
调用的时候用的是这个:
复制代码
如题,求助大佬。
我用其他录音软件录制都不会出现电流声,应该不是设备的问题。
主要代码如下(是一个录音并实时播放的代码):
- import org.lwjgl.openal.AL;
- import org.lwjgl.openal.ALC;
- import org.lwjgl.openal.ALCCapabilities;
- import org.lwjgl.system.MemoryStack;
- import java.nio.ShortBuffer;
- import static org.lwjgl.openal.AL11.*;
- import static org.lwjgl.openal.ALC11.*;
- public class Recorder {
- private long device;
- private long playDevice;
- private long context;
- private int sourcePointer;
- private int bufferPointer;
- public void init() {
- device = alcCaptureOpenDevice(alcGetString(0, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER), 44100, AL_FORMAT_MONO16, 1024);
- playDevice = alcOpenDevice(alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER));
- context = alcCreateContext(playDevice, new int[]{0});
- alcMakeContextCurrent(context);
- ALCCapabilities alcCapabilities = ALC.createCapabilities(playDevice);
- AL.createCapabilities(alcCapabilities);
- }
- public void recordAndPlay() {
- int channels = 1;
- int sampleRate = 44100;
- try(MemoryStack stack = MemoryStack.stackPush()) {
- ShortBuffer rawRecordBuffer = stack.mallocShort(1024);
- bufferPointer = alGenBuffers();
- sourcePointer = alGenSources();
- alListener3f(AL_POSITION, 0F, 0F, 0F);
- alListener3f(AL_VELOCITY, 0F, 0F, 0F);
- alSourcef(sourcePointer, AL_GAIN, 1F);
- alSourcef(sourcePointer, AL_PITCH, 1F);
- alSource3f(sourcePointer, AL_POSITION, 10F, 0F, 0F);
- alSource3f(sourcePointer, AL_VELOCITY, 0F, 0F, 0F);
- alcCaptureStart(device);
- long beginTime = System.currentTimeMillis();
- while(System.currentTimeMillis() - beginTime <= 10_000) {
- int availBuffers = alGetSourcei(sourcePointer, AL_BUFFERS_PROCESSED);
- if(availBuffers > 0) {
- alSourceUnqueueBuffers(sourcePointer);
- alSourceQueueBuffers(sourcePointer, bufferPointer);
- }
- int samplesIn = alcGetInteger(device, ALC_CAPTURE_SAMPLES);
- if(samplesIn > 512) {
- alcCaptureSamples(device, rawRecordBuffer, 512);
- alBufferData(bufferPointer, AL_FORMAT_MONO16, rawRecordBuffer, sampleRate);
- alSourceQueueBuffers(sourcePointer, bufferPointer);
- }
- if(alGetSourcei(sourcePointer, AL_SOURCE_STATE) != AL_PLAYING) {
- alSourcePlay(sourcePointer);
- }
- }
- }
- alcCaptureStop(device);
- }
- public void cleanUp() {
- alDeleteSources(sourcePointer);
- alDeleteBuffers(bufferPointer);
- alcDestroyContext(context);
- alcCloseDevice(playDevice);
- alcCaptureCloseDevice(device);
- }
- }
调用的时候用的是这个:
- public class Main {
- public static void main(String[] args) {
- Recorder recorder = new Recorder();
- recorder.init();
- recorder.recordAndPlay();
- recorder.cleanUp();
- System.out.println("exit");
- }
- }
我个人的猜测是:其他软件有专门的算法来消减这所谓的电流声,而你写的Java代码则是直接原始录入了