From 692a29037827eb2ef7e0640b6d1b347107f4d60d Mon Sep 17 00:00:00 2001 From: kyz Date: Mon, 25 May 2026 09:34:30 +0800 Subject: [PATCH] add mica --- include/odas/source/src_hops.h | 4 + mica.cfg | 439 +++++++++++++++++++++++++++++++++ src/source/src_hops.c | 87 ++++++- 3 files changed, 529 insertions(+), 1 deletion(-) create mode 100644 mica.cfg diff --git a/include/odas/source/src_hops.h b/include/odas/source/src_hops.h index 9e41ca9..355f8dd 100644 --- a/include/odas/source/src_hops.h +++ b/include/odas/source/src_hops.h @@ -71,6 +71,10 @@ typedef struct src_hops_obj { msg_hops_obj * out; + // 新增字段:用于处理自定义TCP数据包格式 + char * packetBuffer; + unsigned int packetBufferSize; + unsigned int lastPacketId; } src_hops_obj; typedef struct src_hops_cfg { diff --git a/mica.cfg b/mica.cfg new file mode 100644 index 0000000..754b96a --- /dev/null +++ b/mica.cfg @@ -0,0 +1,439 @@ +# Configuration file for 16-Microphone Circular Array via TCP + +version = "2.1"; + +# Raw + +raw: +{ + + fS = 48000; + hopSize = 2048; + nBits = 16; + nChannels = 16; + + # Input via TCP socket + interface: { + type = "socket"; + ip = "192.168.31.133"; + port = 6969; + } + +} + +# Mapping + +mapping: +{ + + map: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); + +} + +# General + +general: +{ + + epsilon = 1E-20; + + size: + { + hopSize = 2048; + frameSize = 4096; + }; + + samplerate: + { + mu = 48000; + sigma2 = 0.01; + }; + + speedofsound: + { + mu = 343.0; + sigma2 = 25.0; + }; + + mics = ( + + # Microphone 1 - 0 degrees + { + mu = ( +0.080, +0.000, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 2 - 22.5 degrees + { + mu = ( +0.074, +0.031, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 3 - 45 degrees + { + mu = ( +0.057, +0.057, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 4 - 67.5 degrees + { + mu = ( +0.031, +0.074, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 5 - 90 degrees + { + mu = ( +0.000, +0.080, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 6 - 112.5 degrees + { + mu = ( -0.031, +0.074, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 7 - 135 degrees + { + mu = ( -0.057, +0.057, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 8 - 157.5 degrees + { + mu = ( -0.074, +0.031, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 9 - 180 degrees + { + mu = ( -0.080, +0.000, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 10 - 202.5 degrees + { + mu = ( -0.074, -0.031, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 11 - 225 degrees + { + mu = ( -0.057, -0.057, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 12 - 247.5 degrees + { + mu = ( -0.031, -0.074, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 13 - 270 degrees + { + mu = ( +0.000, -0.080, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 14 - 292.5 degrees + { + mu = ( +0.031, -0.074, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 15 - 315 degrees + { + mu = ( +0.057, -0.057, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + }, + + # Microphone 16 - 337.5 degrees + { + mu = ( +0.074, -0.031, +0.000 ); + sigma2 = ( +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000, +0.000 ); + direction = ( +0.000, +0.000, +1.000 ); + angle = ( 80.0, 100.0 ); + } + + ); + + # Spatial filter to include only a range of direction if required + # (may be useful to remove false detections from the floor) + spatialfilters = ( + + { + direction = ( +0.000, +0.000, +1.000 ); + angle = (80.0, 100.0); + + } + + ); + + nThetas = 181; + gainMin = 0.25; + +}; + +# Stationnary noise estimation + +sne: +{ + + b = 3; + alphaS = 0.1; + L = 150; + delta = 3.0; + alphaD = 0.1; + +} + +# Sound Source Localization + +ssl: +{ + + nPots = 4; + nMatches = 10; + probMin = 0.5; + nRefinedLevels = 1; + interpRate = 4; + + # Number of scans: level is the resolution of the sphere + # and delta is the size of the maximum sliding window + # (delta = -1 means the size is automatically computed) + scans = ( + { level = 2; delta = -1; }, + { level = 4; delta = -1; } + ); + + # Output to export potential sources + potential: { + + # format = "undefined"; + format = "json"; + + interface: { + # type = "blackhole"; + #type = "socket"; ip = "127.0.0.1"; port = 9001; + #type = "terminal"; + type = "socket"; ip = "127.0.0.1"; port = 9001; + }; + + }; + +}; + +# Sound Source Tracking + +sst: +{ + + # Mode is either "kalman" or "particle" + + mode = "kalman"; + + # Add is either "static" or "dynamic" + + add = "dynamic"; + + # Parameters used by both the Kalman and particle filter + + active = ( + { weight = 1.0; mu = 0.4; sigma2 = 0.0025 } + ); + + inactive = ( + { weight = 1.0; mu = 0.25; sigma2 = 0.0025 } + ); + + sigmaR2_prob = 0.0025; + sigmaR2_active = 0.0225; + sigmaR2_target = 0.0025; + Pfalse = 0.1; + Pnew = 0.1; + Ptrack = 0.8; + + theta_new = 0.9; + N_prob = 5; + theta_prob = 0.8; + N_inactive = ( 250, 250, 250, 250 ); + theta_inactive = 0.9; + + # Parameters used by the Kalman filter only + + kalman: { + + sigmaQ = 0.001; + + }; + + # Parameters used by the particle filter only + + particle: { + + nParticles = 1000; + st_alpha = 2.0; + st_beta = 0.04; + st_ratio = 0.5; + ve_alpha = 0.05; + ve_beta = 0.2; + ve_ratio = 0.3; + ac_alpha = 0.5; + ac_beta = 0.2; + ac_ratio = 0.2; + Nmin = 0.7; + + }; + + target: (); + + # Output to export tracked sources + tracked: { + + format = "json"; + + interface: { + # type = "file"; path = "tracks.txt"; + #type = "socket"; ip = "127.0.0.1"; port = 9000; + #type = "terminal"; + type = "socket"; ip = "127.0.0.1"; port = 9000; + }; + + }; + +} + +sss: +{ + + # Mode is either "dds", "dgss" or "dmvdr" + + mode_sep = "dds"; + mode_pf = "ms"; + + gain_sep = 1.0; + gain_pf = 10.0; + + dds: { + + }; + + dgss: { + + mu = 0.01; + lambda = 0.5; + + }; + + dmvdr: { + + }; + + ms: { + + alphaPmin = 0.07; + eta = 0.5; + alphaZ = 0.8; + thetaWin = 0.3; + alphaWin = 0.3; + maxAbsenceProb = 0.9; + Gmin = 0.01; + winSizeLocal = 3; + winSizeGlobal = 23; + winSizeFrame = 256; + + }; + + ss: { + + Gmin = 0.01; + Gmid = 0.9; + Gslope = 10.0; + + }; + + separated: { + + fS = 16000; + hopSize = 128; + nBits = 16; + + interface: { + #type = "file"; + #path = "separated.raw"; + type = "socket"; ip = "127.0.0.1"; port = 10000; + }; + + }; + + postfiltered: { + + fS = 16000; + hopSize = 128; + nBits = 16; + gain = 10.0; + + interface: { + #type = "file"; + #path = "postfiltered.raw"; + type = "socket"; ip = "127.0.0.1"; port = 10010; + }; + + }; + +}; + +classify: +{ + + frameSize = 4096; + winSize = 3; + tauMin = 88; + tauMax = 551; + deltaTauMax = 20; + alpha = 0.3; + gamma = 0.05; + phiMin = 0.5; + r0 = 0.2; + + category: { + + format = "undefined"; + + interface: { + type = "blackhole"; + } + + } + +} diff --git a/src/source/src_hops.c b/src/source/src_hops.c index 99e6e5d..c5d7251 100644 --- a/src/source/src_hops.c +++ b/src/source/src_hops.c @@ -317,6 +317,15 @@ void src_hops_open_interface_socket(src_hops_obj * obj) { printf("Source hops: Cannot connect to server\n"); exit(EXIT_FAILURE); } + +#if 1 + // 分配数据包缓冲区:4字节ID + 16字节保留 + 18通道 × 4096字节 + obj->packetBufferSize = 26 + 18 * 4096; + obj->packetBuffer = (char *) malloc(sizeof(char) * obj->packetBufferSize); + memset(obj->packetBuffer, 0x00, sizeof(char) * obj->packetBufferSize); + obj->lastPacketId = 0; +#endif + } void src_hops_close(src_hops_obj * obj) { @@ -376,7 +385,13 @@ void src_hops_close_interface_pulseaudio(src_hops_obj * obj) { } void src_hops_close_interface_socket(src_hops_obj * obj) { - + +#if 1 + // 释放额外的缓冲区 + if (obj->packetBuffer != NULL) { + free((void *) obj->packetBuffer); + } +#endif close(obj->sid); } @@ -521,6 +536,7 @@ int src_hops_process_interface_pulseaudio(src_hops_obj * obj) { } +#if 0 int src_hops_process_interface_socket(src_hops_obj * obj) { unsigned int nBytesTotal; @@ -543,6 +559,75 @@ int src_hops_process_interface_socket(src_hops_obj * obj) { return rtnValue; } +#else +// 修改数据处理函数 +int src_hops_process_interface_socket(src_hops_obj * obj) { + unsigned int nBytesTotal; + int rtnValue; + + unsigned int iChannel, iSample; + unsigned int nBytesPerChannel = 4096; // 每通道4096字节 + unsigned int nChannelsSrc = 18; // 源数据18通道 + unsigned int nChannelsDst = 16; // 目标16通道 + unsigned int headerSize = 26; // 4字节ID + 16字节保留 + + // 接收完整数据包 + nBytesTotal = recv(obj->sid, obj->packetBuffer, obj->packetBufferSize, MSG_WAITALL); + // printf("Received %u bytes\n", nBytesTotal); + // printf( "FLAG:%02X%02X%02X%02X -- ID:%02X%02X%02X%02X\n", + // (uint8_t)obj->packetBuffer[0], (uint8_t)obj->packetBuffer[1], (uint8_t)obj->packetBuffer[2], (uint8_t)obj->packetBuffer[3], + // (uint8_t)obj->packetBuffer[9], (uint8_t)obj->packetBuffer[10], (uint8_t)obj->packetBuffer[11], (uint8_t)obj->packetBuffer[12]); + + if (nBytesTotal != obj->packetBufferSize) { + rtnValue = -1; + return rtnValue; + } + + // 检查包ID(简单的连续性检查) + unsigned int currentPacketId = *(unsigned int *)obj->packetBuffer; + memcpy(¤tPacketId, &obj->packetBuffer[9], sizeof(unsigned int)); + if (currentPacketId != obj->lastPacketId + 1 && obj->lastPacketId != 0) { + printf("Source hops: Packet loss detected! Expected %u, got %u\n", + obj->lastPacketId + 1, currentPacketId); + } + obj->lastPacketId = currentPacketId; + + // 根据位深度处理数据 + unsigned int bytesPerSample = obj->format->type == format_binary_int08 ? 1 : + obj->format->type == format_binary_int16 ? 2 : + obj->format->type == format_binary_int24 ? 3 : 4; + + unsigned int samplesPerChannel = nBytesPerChannel / bytesPerSample; + + // 确保 hopSize 与 samplesPerChannel 一致 + if (samplesPerChannel != obj->hopSize) { + printf("Source hops: hopSize mismatch (expected %u, got %u)\n", + samplesPerChannel, obj->hopSize); + exit(EXIT_FAILURE); + } + + // 将16通道数据复制到输出缓冲区(交错格式) + for (iSample = 0; iSample < obj->hopSize; iSample++) { + for (iChannel = 0; iChannel < nChannelsDst; iChannel++) { + // 计算源数据位置:头部 + 通道偏移 + 采样偏移 + unsigned int srcOffset = headerSize + + iChannel * nBytesPerChannel + + iSample * bytesPerSample; + // 计算目标位置:交错格式 + unsigned int dstOffset = (iSample * nChannelsDst + iChannel) * bytesPerSample; + + memcpy(&obj->buffer[dstOffset], + &obj->packetBuffer[srcOffset], + bytesPerSample); + } + } + + rtnValue = 0; + return rtnValue; +} + +#endif + void src_hops_process_format_binary_int08(src_hops_obj * obj) { unsigned int iSample; -- 2.34.1