—?—!
本帖最后由 —?—! 于 2022-12-20 14:06 编辑

如题,求助大佬。
我用其他录音软件录制都不会出现电流声,应该不是设备的问题。
主要代码如下(是一个录音并实时播放的代码):
  1. import org.lwjgl.openal.AL;
  2. import org.lwjgl.openal.ALC;
  3. import org.lwjgl.openal.ALCCapabilities;
  4. import org.lwjgl.system.MemoryStack;

  5. import java.nio.ShortBuffer;

  6. import static org.lwjgl.openal.AL11.*;
  7. import static org.lwjgl.openal.ALC11.*;

  8. public class Recorder {
  9.     private long device;
  10.     private long playDevice;
  11.     private long context;
  12.     private int sourcePointer;
  13.     private int bufferPointer;

  14.     public void init() {
  15.         device = alcCaptureOpenDevice(alcGetString(0, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER), 44100, AL_FORMAT_MONO16, 1024);
  16.         playDevice = alcOpenDevice(alcGetString(0, ALC_DEFAULT_DEVICE_SPECIFIER));
  17.         context = alcCreateContext(playDevice, new int[]{0});
  18.         alcMakeContextCurrent(context);
  19.         ALCCapabilities alcCapabilities = ALC.createCapabilities(playDevice);
  20.         AL.createCapabilities(alcCapabilities);
  21.     }

  22.     public void recordAndPlay() {
  23.         int channels = 1;
  24.         int sampleRate = 44100;
  25.         try(MemoryStack stack = MemoryStack.stackPush()) {
  26.             ShortBuffer rawRecordBuffer = stack.mallocShort(1024);

  27.             bufferPointer = alGenBuffers();
  28.             sourcePointer = alGenSources();

  29.             alListener3f(AL_POSITION, 0F, 0F, 0F);
  30.             alListener3f(AL_VELOCITY, 0F, 0F, 0F);
  31.             alSourcef(sourcePointer, AL_GAIN, 1F);
  32.             alSourcef(sourcePointer, AL_PITCH, 1F);
  33.             alSource3f(sourcePointer, AL_POSITION, 10F, 0F, 0F);
  34.             alSource3f(sourcePointer, AL_VELOCITY, 0F, 0F, 0F);

  35.             alcCaptureStart(device);
  36.             long beginTime = System.currentTimeMillis();
  37.             while(System.currentTimeMillis() - beginTime <= 10_000) {
  38.                 int availBuffers = alGetSourcei(sourcePointer, AL_BUFFERS_PROCESSED);
  39.                 if(availBuffers > 0) {
  40.                     alSourceUnqueueBuffers(sourcePointer);
  41.                     alSourceQueueBuffers(sourcePointer, bufferPointer);
  42.                 }
  43.                 int samplesIn = alcGetInteger(device, ALC_CAPTURE_SAMPLES);
  44.                 if(samplesIn > 512) {
  45.                     alcCaptureSamples(device, rawRecordBuffer, 512);
  46.                     alBufferData(bufferPointer, AL_FORMAT_MONO16, rawRecordBuffer, sampleRate);
  47.                     alSourceQueueBuffers(sourcePointer, bufferPointer);
  48.                 }
  49.                 if(alGetSourcei(sourcePointer, AL_SOURCE_STATE) != AL_PLAYING) {
  50.                     alSourcePlay(sourcePointer);
  51.                 }
  52.             }
  53.         }
  54.         alcCaptureStop(device);
  55.     }

  56.     public void cleanUp() {
  57.         alDeleteSources(sourcePointer);
  58.         alDeleteBuffers(bufferPointer);
  59.         alcDestroyContext(context);
  60.         alcCloseDevice(playDevice);
  61.         alcCaptureCloseDevice(device);
  62.     }
  63. }
复制代码


调用的时候用的是这个:
  1. public class Main {
  2.     public static void main(String[] args) {
  3.         Recorder recorder = new Recorder();
  4.         recorder.init();
  5.         recorder.recordAndPlay();
  6.         recorder.cleanUp();
  7.         System.out.println("exit");
  8.     }
  9. }
复制代码

小张up
我个人的猜测是:其他软件有专门的算法来消减这所谓的电流声,而你写的Java代码则是直接原始录入了

第一页 上一页 下一页 最后一页