2013-02-22 1 views
2

현재 Alsa를 사용하여 오디오 파일을 재생하는 방법을 배우고 있습니다. 나는 그것을 가장 많이 가지고있는 것처럼 보입니다. 파일이로드되고 재생됩니다.하지만 정확하게 재생하려면 반으로 줄여야합니다. 왜 그런가요? 어떻게 고칠 수 있습니까? Alsa가 주파수의 2 배에서 vorbis를 재생 중입니다.

using Alsa; 
using Gee; 
using OGG; 


namespace SoundTest 
{ 
    public class Sound : Object 
    { 
     private PcmDevice sound_device; 
     private char[] sound_data; 

     private uint8[] alsa_data; 
     private long bit_rate; 

     public class Sound() 
     { 
     } 

     ~Sound() 
     { 
      sound_device.drain(); 
      sound_device.close(); 
     } 

     public void init() 
     { 
      read_sound_file(); 
      setup_sound_player(); 
     } 

     public void play_sound_file() 
     { 
      sound_device.writei(alsa_data, sound_device.bytes_to_frames(alsa_data.length)); 
     } 

     private uint8[] convert_data(char[] buffer) 
     { 
      uint8[] conversion; 

      // Just need to convert to an array of uint8s. 
      conversion = new uint8[buffer.length]; 
      for (int i = 0; i < buffer.length; i++) 
      { 
       conversion[i] = (uint8)buffer[i]; 
      } 

      return conversion; 
     } 

     private void setup_sound_player() 
     { 
      // Open the sound device. 
      if (PcmDevice.open(out sound_device, "default", PcmStream.PLAYBACK, 0) != 0) 
      { 
       critical("Unable to open a sound device\n"); 
       return; 
      } 

      // Set the parameters to use for playback. 
      if (sound_device.set_params(PcmFormat.S16_LE, PcmAccess.RW_INTERLEAVED, 2, (int)bit_rate/2, 1, 500000) != 0) 
      { 
       critical("Could not set params properly\n"); 
       return; 
      } 

      // Prepare the device for playback. 
      if (sound_device.prepare() != 0) 
      { 
       critical("Could not prepare the device\n"); 
       return; 
      } 
     } 

     private void read_sound_file() 
     { 
      char[] buffer; 
      int stream; 
      long bytesRead; 
      string filename; 
      File inputFile; 
      Info? soundInfo = null; 
      Vorbis soundFile; 
      ArrayList<char> data; 

      filename = "./Median_test.ogg"; 

      // Make sure the file exists. 
      inputFile = File.new_for_path(filename); 
      if (inputFile.query_exists()) 
      { 
       // Set the file to be read. 
       message("Reading file: %s\n", filename); 
       soundFile = Vorbis(filename); 

       // Get the information about the sound file. 
       soundInfo = soundFile.get_info(-1); 

       if (soundInfo != null) 
       { 
        stdout.printf("Version: %d\nChannels: %d\nRate: %ld\nBitrate: %ld, %ld, %ld\n\n", 
            soundInfo.version, soundInfo.channels, 
            soundInfo.rate, soundInfo.bitrate_lower, 
            soundInfo.bitrate_nominal, soundInfo.bitrate_upper); 

        // Store the bitrate information of the sound file. 
        bit_rate = soundInfo.rate; 
       } 

       // Read the data from the file. Here I read a small array worth of 
       // data and then store it in an array list. I then need to turn 
       // that array list into a single array. I wish there was a function 
       // to do it from the list, but I'll just loop. 
       buffer = new char[4096]; 
       stream = 0; 
       data = new ArrayList<char>(); 
       bytesRead = soundFile.read(buffer, 0, 2, 1, ref stream); 
       while (bytesRead > 0) 
       { 
        for (int i = 0; i < bytesRead; i++) 
        { 
         data.add(buffer[i]); 
        } 

        bytesRead = soundFile.read(buffer, 0, 2, 1, ref stream); 
       } 
       stdout.printf("Bytes read: %ld\n", bytesRead); 

       sound_data = new char[data.size]; 
       for (int i = 0; i < data.size; i++) 
       { 
        sound_data[i] = data[i]; 
       } 

       // Now convert the vorbis data into the type of data alsa needs. 
       alsa_data = convert_data(sound_data); 
      } 
      else 
      { 
       critical("No such file:\n\t%s\n\n", filename); 
      } 
     } 

     public static int main(string[] args) 
     { 
      Sound test; 

      stdout.printf("Starting Sound Test\n"); 

      test = new Sound(); 
      test.init(); 
      test.play_sound_file(); 

      stdout.printf("Done\n"); 


      return 0; 
     } 
    } 
} 

당신이 줄 수있는 모든 도움을 주셔서 감사합니다

는 여기에 내가 현재 사용하고있는 발라 코드입니다.

+0

해당 파일의 속도는 무엇입니까? –

+0

@CL 파일은 44100의 속도를 보여줍니다. 테스트를 위해 사용하고있는 파일은 [here] (http://en.wikipedia.org/wiki/File:Median_test.ogg)에서 찾을 수 있습니다. –

답변

2

두 채널에 대해 사운드 장치를 구성하고 있지만 그 파일에는 실제로 하나만 있습니다.

+0

감사합니다. 그게 내 문제 야. 좋아, 그 정보로 사운드 소스 구조를 만들 필요가있다. 다시 감사합니다. –

관련 문제