import pyaudio
import wave
import threading
import time

class Recorder:
    def __init__(self, filename='recording.wav', chunk=1024, format=pyaudio.paInt16, channels=2, rate=44100):
        self.filename = filename
        self.chunk = chunk
        self.format = format
        self.channels = channels
        self.rate = rate
        self.frames = []
        self.p = pyaudio.PyAudio()
        self.is_recording = False
        self.record_thread = None

    def start_recording(self):
        if self.is_recording:
            print("Recording is already in progress.")
            return

        print("Recording started.")
        self.is_recording = True
        self.frames = []
        self.stream = self.p.open(format=self.format,
                                  channels=self.channels,
                                  rate=self.rate,
                                  input=True,
                                  frames_per_buffer=self.chunk)
        self.record_thread = threading.Thread(target=self._record)
        self.record_thread.start()

    def stop_recording(self):
        if not self.is_recording:
            print("No recording in progress.")
            return

        print("Recording stopped.")
        self.is_recording = False
        self.stream.stop_stream()
        self.stream.close()
        self.record_thread.join()
        wf = wave.open(self.filename, 'wb')
        wf.setnchannels(self.channels)
        wf.setsampwidth(self.p.get_sample_size(self.format))
        wf.setframerate(self.rate)
        wf.writeframes(b''.join(self.frames))
        wf.close()

    def _record(self):
        while self.is_recording:
            data = self.stream.read(self.chunk)
            self.frames.append(data)

if __name__ == "__main__":
    filename = input("Enter the filename to save recording (default is 'recording.wav'): ").strip() or 'recording.wav'
    recorder = Recorder(filename=filename)
    
    print("Press 'r' to start recording, 's' to stop recording, and 'q' to quit.")
    while True:
        command = input("> ").strip().lower()
        if command == 'r':
            recorder.start_recording()
        elif command == 's':
            recorder.stop_recording()
            print("Recording saved to", filename)
        elif command == 'q':
            if recorder.is_recording:
                recorder.stop_recording()
                print("Recording saved to", filename)
            break
        else:
            print("Invalid command. Please enter 'r' to start recording, 's' to stop recording, or 'q' to quit.")
        time.sleep(0.1)  # sleep to avoid high CPU usage in the loop

跟sphinx一样。只支持linux系统。
https://manual.manticoresearch.com/Server_settings/Special_suffixes
可以支持简单的PHP脚本

#!/usr/bin/php
...
<?php for ($i=1; $i<=6; $i++) { ?>
table test_<?=$i?> {
  type = rt
  path = /var/lib/manticore/data/test_<?=$i?>
  rt_field = subject
  ...
 }
 <?php } ?>
 ...

 <?php
 $confd_folder='/etc/manticore.conf.d/';
 $files = scandir($confd_folder);
 foreach($files as $file)
 {
         if(($file == '.') || ($file =='..'))
         {} else {
                 $fp = new SplFileInfo($confd_folder.$file);
                 if('conf' == $fp->getExtension()){
                         include ($confd_folder.$file);
                 }
         }
 }
 ?>

如果查询语句返回结果太多的话,几千万,会出现lost connction的情况。

解决办法,分次查询:

其中uid是自增ID
source 索引名 : 数据库名{
        sql_query_range = SELECT MIN(uid),MAX(uid) FROM 表名
        sql_range_step = 1000
        sql_query = SELECT *,53 AS table_id FROM 表名 WHERE uid>=$start AND uid<=$end
        sql_attr_uint           = table_id
}

经过测试改成1000就可以了。

参考官方的安装方法。直接apt install.

Ubuntu如下:其他系统自查。https://manual.manticoresearch.com/Installation/Debian_and_Ubuntu#Supported-releases:

wget https://repo.manticoresearch.com/manticore-repo.noarch.deb
sudo dpkg -i manticore-repo.noarch.deb
sudo apt update

sudo apt install manticore manticore-extra

安装完了之后无法连接MYSQL。
报错:MySQL source wasn’t initialized. Wrong name in dlopen?
搜了下:
https://manticoresearch.com/blog/mysql-source-wasnt-initialized-wrong-name-in-dlopen/
直接

apt install default-libmysqlclient-dev

安装完之后,好像还是报错,这次报错,连接mysql的sock文件不对,直接改my.cnf:

#socket = /tmp/mysql.sock
socket = /var/run/mysqld/mysqld.sock

这下可以了。

另外就是manticore的conf不支持include语法,如果索引多的话,会很长很长,几千行。
后来发现其实是支持的,参见https://kele.im/index.php/archives/318/