1313#define GROUP_IP " 239.168.123.161"
1414#define PORT 5555
1515
16- #define WAV_SECOND 5 // record seconds
16+ #define WAV_SECOND 5 // record seconds
1717#define WAV_LEN (16000 * 2 * WAV_SECOND)
18+ #define CHUNK_SIZE 96000 // 3 seconds
1819int sock;
1920
2021void asr_handler (const void *msg) {
@@ -29,13 +30,14 @@ std::string get_local_ip_for_multicast() {
2930
3031 getifaddrs (&ifaddr);
3132 for (ifa = ifaddr; ifa != nullptr ; ifa = ifa->ifa_next ) {
32- if (!ifa->ifa_addr || ifa->ifa_addr ->sa_family != AF_INET) continue ;
33- getnameinfo (ifa->ifa_addr , sizeof (struct sockaddr_in ), host, NI_MAXHOST, NULL , 0 , NI_NUMERICHOST);
34- std::string ip (host);
35- if (ip.find (" 192.168.123." ) == 0 ) {
36- result = ip;
37- break ;
38- }
33+ if (!ifa->ifa_addr || ifa->ifa_addr ->sa_family != AF_INET) continue ;
34+ getnameinfo (ifa->ifa_addr , sizeof (struct sockaddr_in ), host, NI_MAXHOST,
35+ NULL , 0 , NI_NUMERICHOST);
36+ std::string ip (host);
37+ if (ip.find (" 192.168.123." ) == 0 ) {
38+ result = ip;
39+ break ;
40+ }
3941 }
4042 freeifaddrs (ifaddr);
4143 return result;
@@ -52,7 +54,7 @@ void thread_mic(void) {
5254 ip_mreq mreq{};
5355 inet_pton (AF_INET, GROUP_IP, &mreq.imr_multiaddr );
5456 std::string local_ip = get_local_ip_for_multicast ();
55- std::cout << " local ip: " << local_ip << std::endl;
57+ std::cout << " local ip: " << local_ip << std::endl;
5658 mreq.imr_interface .s_addr = inet_addr (local_ip.c_str ());
5759 setsockopt (sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof (mreq));
5860
@@ -125,16 +127,30 @@ int main(int argc, char const *argv[]) {
125127
126128 std::cout << " wav file sample_rate = " << sample_rate
127129 << " num_channels = " << std::to_string (num_channels)
128- << " filestate =" << filestate << std::endl;
130+ << " filestate =" << filestate << " filesize = " << pcm.size ()
131+ << std::endl;
129132
130133 if (filestate && sample_rate == 16000 && num_channels == 1 ) {
131- client.PlayStream (
132- " example" , std::to_string (unitree::common::GetCurrentTimeMillisecond ()),
133- pcm);
134- std::cout << " start play stream" << std::endl;
135- unitree::common::Sleep (3 );
136- std::cout << " stop play stream" << std::endl;
137- ret = client.PlayStop (" example" );
134+ size_t total_size = pcm.size ();
135+ size_t offset = 0 ;
136+ int chunk_index = 0 ;
137+ std::string stream_id =
138+ std::to_string (unitree::common::GetCurrentTimeMillisecond ());
139+
140+ while (offset < total_size) {
141+ size_t remaining = total_size - offset;
142+ size_t current_chunk_size =
143+ std::min (static_cast <size_t >(CHUNK_SIZE), remaining);
144+ std::vector<uint8_t > chunk (pcm.begin () + offset,
145+ pcm.begin () + offset + current_chunk_size);
146+ client.PlayStream (" example" , stream_id, chunk);
147+ unitree::common::Sleep (1 );
148+ std::cout << " Playing size: " << offset << std::endl;
149+ offset += current_chunk_size;
150+ }
151+
152+ ret = client.PlayStop (stream_id); // stop playback after transmission ends
153+
138154 } else {
139155 std::cout << " audio file format error, please check!" << std::endl;
140156 }
0 commit comments