[一星]请人写一个夏季驱蚊的脚本

除了美化之外,还可以来尝试挑战一下任务
回复
moke520
帖子: 1
注册时间: 2011-11-17 10:08

Re: [一星]请人写一个夏季驱蚊的脚本

#91

帖子 moke520 » 2011-11-17 10:10

我只要求: 声波不要搞得太强烈,不能搞到影响身体健康~
头像
xyz627
帖子: 5
注册时间: 2011-12-11 20:30

Re: [一星]请人写一个夏季驱蚊的脚本

#92

帖子 xyz627 » 2011-12-12 11:09

高手写一个冬天取暖的脚本吧 :em09
头像
cxbii
帖子: 492
注册时间: 2011-02-06 1:38

Re: [一星]请人写一个夏季驱蚊的脚本

#93

帖子 cxbii » 2011-12-30 12:10

:em03 我等着看成果呢
zlming
帖子: 68
注册时间: 2009-04-23 15:37

Re: [一星]请人写一个夏季驱蚊的脚本

#94

帖子 zlming » 2012-04-05 13:58

不知道你说的那个频率多大,我看这种稀奇古怪的还是去国外的网站上发帖,得到的答案会多些。
头像
redhatlinux10
帖子: 773
注册时间: 2008-01-22 23:24
来自: 三亚
联系:

Re: [一星]请人写一个夏季驱蚊的脚本

#95

帖子 redhatlinux10 » 2012-04-05 16:14

电子驱蚊基本是个骗人的把戏。
http://www.soomal.com/doc/10100002174.htm
头像
月下叹逍遥
论坛版主
帖子: 33994
注册时间: 2010-10-07 14:23
系统: Archdows10
来自: 某系某星某洲某国某省某市
联系:

Re: [一星]请人写一个夏季驱蚊的脚本

#96

帖子 月下叹逍遥 » 2012-06-22 17:48

咳咳,两年了,乃们这群脚本帝呢???
浮生七十今三十,从此凄惶未可知
头像
qy117121
论坛版主
帖子: 50529
注册时间: 2007-12-14 13:40
系统: Winbuntu
来自: 志虚国乌由市
联系:

Re: [一星]请人写一个夏季驱蚊的脚本

#97

帖子 qy117121 » 2012-06-22 17:57

月下叹逍遥 写了:咳咳,两年了,乃们这群脚本帝呢???
死南瓜,挖坟
渠月 · QY   
本人只会灌水,不负责回答问题
无聊可以点一下→ http://u.nu/ubuntu

Ubuntu 20.04 快速设置指南,请配合浏浏览器自动翻译使用

邮箱 chuan@ubuntu.org.cn
头像
月下叹逍遥
论坛版主
帖子: 33994
注册时间: 2010-10-07 14:23
系统: Archdows10
来自: 某系某星某洲某国某省某市
联系:

Re: [一星]请人写一个夏季驱蚊的脚本

#98

帖子 月下叹逍遥 » 2012-06-22 18:03

这个区里全是坟。。。。
浮生七十今三十,从此凄惶未可知
头像
monk
帖子: 19507
注册时间: 2010-01-28 10:45

Re: [一星]请人写一个夏季驱蚊的脚本

#99

帖子 monk » 2012-06-22 18:05

南瓜来写吧

*********宁静致远*********
如果我说的有什么不对,请看签名第一行
acifehep
帖子: 1
注册时间: 2012-06-14 7:14

Re: [??]????????????

#100

帖子 acifehep » 2012-06-23 10:19

You have thought up such matchless answer?
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

Re: [一星]请人写一个夏季驱蚊的脚本

#102

帖子 oneleaf » 2012-06-29 10:17

直接安装beep
sudo apt-get install beep
beep -f 频率 -l 持续时间毫秒

但没有效果的。
头像
Diagrams
帖子: 44
注册时间: 2012-08-12 10:36

Re: [一星]请人写一个夏季驱蚊的脚本

#103

帖子 Diagrams » 2012-08-12 11:08

都高人啊,能不能写个能招来美女的脚本呢?
thunderlast
帖子: 34
注册时间: 2010-09-18 21:21

Re: [一星]请人写一个夏季驱蚊的脚本

#104

帖子 thunderlast » 2012-09-01 20:26

参考StdAudio.java,改写了测试部分。
网上查的驱蚊频率是6000hz左右,参见http://zhidao.baidu.com/question/9422139.html
专业驱蚊的文章请看这里:http://www.guokr.com/article/49122/
未测试驱蚊效果,亢余代码也很多。简化后估计几十行。
准备吵死大家!!! :em02

代码: 全选

/*************************************************************************
 *  Compilation:  javac StdAudio.java
 *  Execution:    java StdAudio
 *  
 *  Simple library for reading, writing, and manipulating .wav files.

 *
 *  Limitations
 *  -----------
 *    - Does not seem to work properly when reading .wav files from a .jar file.
 *    - Assumes the audio is monaural, with sampling rate of 44,100.
 *
 *************************************************************************/

import java.applet.*;
import java.io.*;
import java.net.*;
import javax.sound.sampled.*;

/**
 *  <i>Standard audio</i>. This class provides a basic capability for
 *  creating, reading, and saving audio. 
 *  <p>
 *  The audio format uses a sampling rate of 44,100 (CD quality audio), 16-bit, monaural.
 *
 *  <p>
 *  For additional documentation, see <a href="http://introcs.cs.princeton.edu/15inout">Section 1.5</a> of
 *  <i>Introduction to Programming in Java: An Interdisciplinary Approach</i> by Robert Sedgewick and Kevin Wayne.
 */
public final class StdAudio {

    /**
     *  The sample rate - 44,100 Hz for CD quality audio.
     */
    public static final int SAMPLE_RATE = 44100;

    private static final int BYTES_PER_SAMPLE = 2;                // 16-bit audio
    private static final int BITS_PER_SAMPLE = 16;                // 16-bit audio
    private static final double MAX_16_BIT = Short.MAX_VALUE;     // 32,767
    private static final int SAMPLE_BUFFER_SIZE = 4096;


    private static SourceDataLine line;   // to play the sound
    private static byte[] buffer;         // our internal buffer
    private static int bufferSize = 0;    // number of samples currently in internal buffer

    // do not instantiate
    private StdAudio() { }

   
    // static initializer
    static { init(); }

    // open up an audio stream
    private static void init() {
        try {
            // 44,100 samples per second, 16-bit audio, mono, signed PCM, little Endian
            AudioFormat format = new AudioFormat((float) SAMPLE_RATE, BITS_PER_SAMPLE, 1, true, false);
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(format, SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE);
            
            // the internal buffer is a fraction of the actual buffer size, this choice is arbitrary
            // it gets divided because we can't expect the buffered data to line up exactly with when
            // the sound card decides to push out its samples.
            buffer = new byte[SAMPLE_BUFFER_SIZE * BYTES_PER_SAMPLE/3];
        } catch (Exception e) {
            System.out.println(e.getMessage());
            System.exit(1);
        }

        // no sound gets made before this call
        line.start();
    }


    /**
     * Close standard audio.
     */
    public static void close() {
        line.drain();
        line.stop();
    }
    
    /**
     * Write one sample (between -1.0 and +1.0) to standard audio. If the sample
     * is outside the range, it will be clipped.
     */
    public static void play(double in) {

        // clip if outside [-1, +1]
        if (in < -1.0) in = -1.0;
        if (in > +1.0) in = +1.0;

        // convert to bytes
        short s = (short) (MAX_16_BIT * in);
        buffer[bufferSize++] = (byte) s;
        buffer[bufferSize++] = (byte) (s >> 8);   // little Endian

        // send to sound card if buffer is full        
        if (bufferSize >= buffer.length) {
            line.write(buffer, 0, buffer.length);
            bufferSize = 0;
        }
    }

    /**
     * Write an array of samples (between -1.0 and +1.0) to standard audio. If a sample
     * is outside the range, it will be clipped.
     */
    public static void play(double[] input) {
        for (int i = 0; i < input.length; i++) {
            play(input[i]);
        }
    }

    /**
     * Read audio samples from a file (in .wav or .au format) and return them as a double array
     * with values between -1.0 and +1.0.
     */
    public static double[] read(String filename) {
        byte[] data = readByte(filename);
        int N = data.length;
        double[] d = new double[N/2];
        for (int i = 0; i < N/2; i++) {
            d[i] = ((short) (((data[2*i+1] & 0xFF) << 8) + (data[2*i] & 0xFF))) / ((double) MAX_16_BIT);
        }
        return d;
    }




    /**
     * Play a sound file (in .wav or .au format) in a background thread.
     */
    public static void play(String filename) {
        URL url = null;
        try {
            File file = new File(filename);
            if (file.canRead()) url = file.toURI().toURL();
        }
        catch (MalformedURLException e) { e.printStackTrace(); }
        // URL url = StdAudio.class.getResource(filename);
        if (url == null) throw new RuntimeException("audio " + filename + " not found");
        AudioClip clip = Applet.newAudioClip(url);
        clip.play();
    }

    /**
     * Loop a sound file (in .wav or .au format) in a background thread.
     */
    public static void loop(String filename) {
        URL url = null;
        try {
            File file = new File(filename);
            if (file.canRead()) url = file.toURI().toURL();
        }
        catch (MalformedURLException e) { e.printStackTrace(); }
        // URL url = StdAudio.class.getResource(filename);
        if (url == null) throw new RuntimeException("audio " + filename + " not found");
        AudioClip clip = Applet.newAudioClip(url);
        clip.loop();
    }


    // return data as a byte array
    private static byte[] readByte(String filename) {
        byte[] data = null;
        AudioInputStream ais = null;
        try {

            // try to read from file
            File file = new File(filename);
            if (file.exists()) {
                ais = AudioSystem.getAudioInputStream(file);
                data = new byte[ais.available()];
                ais.read(data);
            }

            // try to read from URL
            else {
                URL url = StdAudio.class.getResource(filename);
                ais = AudioSystem.getAudioInputStream(url);
                data = new byte[ais.available()];
                ais.read(data);
            }
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            throw new RuntimeException("Could not read " + filename);
        }

        return data;
    }



    /**
     * Save the double array as a sound file (using .wav or .au format).
     */
    public static void save(String filename, double[] input) {

        // assumes 44,100 samples per second
        // use 16-bit audio, mono, signed PCM, little Endian
        AudioFormat format = new AudioFormat(SAMPLE_RATE, 16, 1, true, false);
        byte[] data = new byte[2 * input.length];
        for (int i = 0; i < input.length; i++) {
            int temp = (short) (input[i] * MAX_16_BIT);
            data[2*i + 0] = (byte) temp;
            data[2*i + 1] = (byte) (temp >> 8);
        }

        // now save the file
        try {
            ByteArrayInputStream bais = new ByteArrayInputStream(data);
            AudioInputStream ais = new AudioInputStream(bais, format, input.length);
            if (filename.endsWith(".wav") || filename.endsWith(".WAV")) {
                AudioSystem.write(ais, AudioFileFormat.Type.WAVE, new File(filename));
            }
            else if (filename.endsWith(".au") || filename.endsWith(".AU")) {
                AudioSystem.write(ais, AudioFileFormat.Type.AU, new File(filename));
            }
            else {
                throw new RuntimeException("File format not supported: " + filename);
            }
        }
        catch (Exception e) {
            System.out.println(e);
            System.exit(1);
        }
    }




   /***********************************************************************
    * sample test client
    ***********************************************************************/

    // create a note (sine wave) of the given frequency 囗囗囗囗, for the given
    // duration (seconds) scaled to the given volume (amplitude)
    private static double[] note(double hz, double duration, double amplitude) {
        int N = (int) (StdAudio.SAMPLE_RATE * duration);
        double[] a = new double[N+1];
        for (int i = 0; i <= N; i++)
            a[i] = amplitude * Math.sin(2 * Math.PI * i * hz / StdAudio.SAMPLE_RATE);
        return a;
    }

    /**
     * Test client - play an A major scale to standard audio.
     */
    public static void main(String[] args) {
        
        // 6000.0 Hz for getting out of mosquito
        double freq = 6000.0;
        double time = 10.0; // 10 seconds

        StdAudio.play(note(freq, time, 0.5));    

        // need to call this in non-interactive stuff so the program doesn't terminate
        // until all the sound leaves the speaker.
        StdAudio.close(); 

        // need to terminate a Java program with sound
        System.exit(0);
    }
}
头像
liu滔
帖子: 7212
注册时间: 2010-10-09 23:01

Re: [一星]请人写一个夏季驱蚊的脚本

#105

帖子 liu滔 » 2012-09-02 0:44

楼上是第一个实作的人吧 :em06
回复