Just another WordPress site

Sending and playing microphone audio over network


Giới thiệu
Ví dụ này sẽ chỉ cho bạn cách nhận tín hiệu từ microphone và truyền (stream) tới một máy khác qua giao thức UDP. Chương trình ví dụ có thể thực hiện như một chiếc điện thoại, nếu cả 2 máy đều có thể nghe và gởi tín hiệu microphone cho nhau. Ngoài ra cũng cần một project tương tự để chứa UDP server mà chúng ta cần để gởi nhậu audio và nén lại với codec g711. Chỉ dùng giao thức UDP thì không phải là cách tốt nhất để truyền dữ liệu dạng audio, RTP là cách tốt hơn. RTP thêm QoS vào việc truyền tải audio, nên bạn có thể biết được bao nhiêu gói (packets) bị mất và có thể sắp xếp lại những gói đó. Tôi sẽ thử thêm một ví dụ về giao thức RTP lúc khác.
The package contains:

  • LumiSoft.Media – Audio related API (được dùng trong ví dụ)
  • LumiSoft.Net – UDP server, G711 codec
  • Ví dụ mẫu

Code
1. WaveIn là lớp cung cấp cách đơn giản để nhận tín hiệu từ microphone.
Tất cả việc bạn phải làm là:
WavIn.Devices trả về tất cả các thiết bị sẵn sàng mà chúng ta có thể nhận dữ liệu.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// <summary>
 
/// Application main class.
 
/// </summary>
 
public class Test
{
    private WavIn m_pSoundReceiver = null;
 
    /// <summary>
 
    /// Default constructor.
 
    /// </summary>
 
    public Test()
    {
        // G711 needs 8KHZ 16 bit 1 channel audio, 
 
        // 400kb buffer gives us 25ms audio frame.
 
        m_pSoundReceiver = new WavIn(WavIn.Devices[0],8000,16,1,400);
        m_pSoundReceiver.BufferFull += new BufferFullHandler 
                                         (m_pSoundReceiver_BufferFull);
        m_pSoundReceiver.Start();
    }
 
    /// <summary>
 
    /// This method is called when recording buffer is full 
 
    /// and we need to process it.
 
    /// </summary>
 
    /// <param name="buffer">Recorded data.</param>
 
    private void m_pSoundReceiver_BufferFull(byte[] buffer)
    {
        // Just store audio data or stream it over the network ... 
 
    }
}

WaveOut – lớp cung cấp các method cho việc stream audio.
Bạn chỉ cần làm 1 việc đó là gọi method waveoutInstance.Play
Lưu ý: Chất lượng âm thanh phụ thuộc vào độ trễ của mạng (network delay).

Theo codeproject.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Powered by WordPress | Designed by Elegant Themes