RetroGame

如何設定聲音大小


雖然RetroGame不是開源的掌機,不過為了可以讓它更趨近完美,司徒最終逆向出廠預設的模擬器,藉此得知如何控制聲音,整理後的程式如下:

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/soundcard.h>
#include <sys/ioctl.h>

#define MIXER_DEV "/dev/mixer"

typedef enum {
  AUDIO_VOLUME_SET,
  AUDIO_VOLUME_GET,
} audio_volume_action;

int audio_volume(audio_volume_action action, long* outvol)
{
  int ret = 0;
  int fd, devs;

  if((fd = open(MIXER_DEV, O_WRONLY)) > 0){
    if(action == AUDIO_VOLUME_SET){
      if(*outvol < 0 || *outvol > 100){
        return -2;
      }
      
      *outvol = (*outvol << 8) | *outvol;
      ioctl(fd, SOUND_MIXER_WRITE_VOLUME, outvol);
    }
    else if(action == AUDIO_VOLUME_GET){
      ioctl(fd, SOUND_MIXER_READ_VOLUME, outvol);
      *outvol = *outvol & 0xff;
    }
    close(fd);
    return 0;
  }
  return -1;
}

int main(void)
{
  long ret=0, vol=0;

  vol = -1;
  ret = audio_volume(AUDIO_VOLUME_GET, &vol);
  printf("old volume: %d\n", vol);

  vol = 100;
  ret = audio_volume(AUDIO_VOLUME_SET, &vol);
  printf("set volume to %d\n", vol);
  
  vol = -1;
  ret = audio_volume(AUDIO_VOLUME_GET, &vol);
  printf("new volume is: %d\n", vol);
  return 0;
}


返回上一頁