日期:2014-05-20  浏览次数:20627 次

J2me随机频谱

前段时间,想在手机播放音乐的时候,画出一个频谱,但是j2me手机上面做基于傅立叶系数的技术基本不可能,所以真的频谱用j2me去做基本是不可能,所以看到市面上很多带频谱的j2me播放器都是假的频谱,是一序列的随机数,如何做个更像真的一样呢?一种办法是采样:录几种不能风格的音频数据,然后用这些数据来画;另外就是优化随机算法。

下面是今天花了点时间写的随即算法:

public ToneWaveCanvas() {
??????? this.data = new int[this.rowNum];
??????? this.dataBK = new int[this.rowNum];
??????? setFullScreenMode(true);
??????? this.random = new Random();
??????? this.font = Font.getDefaultFont();
??????? this.playThread = new Thread(this);
??????? init();
??? }

??? public final void paint(Graphics g) {
??????? if (isShown()) {
??????????? try {
??????????????? if (isPlaying) {
??????????????????? getWaveData();
??????????????? } else {
??????????????????? clearWaveData();
??????????????? }
??????????????? g.setColor(0, 0, 0);
??????????????? g.fillRect(0, 0, getWidth(), getHeight());
??????????????? g.setColor(0, 255, 255);
??????????????? for (int j = 0; j < this.rowNum; ++j) {
??????????????????? g.fillRect(j * rthyRowWidth, this.startY - this.data[j], rthyRowWidth - 1, this.data[j]);
??????????????????? g.drawLine(j * rthyRowWidth, this.startY - this.dataBK[j], j * rthyRowWidth + rthyRowWidth - 2, this.startY - this.dataBK[j]);
??????????????? }
??????????????? g.setColor(0, 132, 45);
??????????????? for (int k = 0; k < this.rowNum; ++k) {
??????????????????? g.drawLine(k * rthyRowWidth, this.startY - this.dataBK[k], k * rthyRowWidth + 6, this.startY - this.dataBK[k]);
??????????????? }
??????????????? return;
??????????? } catch (Exception e) {
??????????? }
??????? }
??? }

??? public final void run() {
??????? while (true) {
??????????? repaint();
??????????? if (!isPlaying) {
??????????????? synchronized (lock) {
??????????????????? try {
??????????????????????? lock.wait();
??????????????????? } catch (InterruptedException ex) {
??????????????????????? ex.printStackTrace();
??????????????????? }
??????????????? }
??????????? }
??????????? try {
??????????????? Thread.sleep(100L);
??????????? } catch (Exception e) {
??????????? }
??????? }
??? }

??? public final void start() {
??????? this.isPlaying = true;
??????? synchronized (lock) {
??????????? lock.notify();
??????? }
??? }

??? public void stop() {
??????? this.isPlaying = false;
??? }

??? private int getRandomIndex(int mother) {
??????? int j;
??????? if ((j = this.random.nextInt() % mother) < 0) {
??????????? j *= -1;
??????? }
??????? return j;
??? }

??? private void init() {
??????? this.rowNum = (getWidth() / rthyRowWidth);
??????? this.startY = (getHeight() / 2 - 10);
??????? this.tempH1 = (this.startY / 45);
??????? this.tempH2 = (this.startY / 28);
??????? this.tempH3 = (this.startY / 8);
??????? this.data = null;
??????? this.dataBK = null;
??????? this.data = new int[this.rowNum];
??????? this.dataBK = new int[this.rowNum];
??????? this.startY += 10;
??? }

??? private void getWaveData() {
??????? for (int j = 0; j < this.data.length; ++j) {
??????????? this.data[j] -= this.tempH2;
??????????? if (this.data[j] < 0) {
??????????????? this.data[j] = 0;
??????????? }
??????? }
??????? for (int k = 0; k < getRandomIndex(this.rowNum); ++k) {
??????????? int j = getRandomIndex(this.rowNum);
??????????? this.data[j] += this.tempH3;
??????????? if (data[j] <= 10) {
??????????????? data[j] = (startY >> 2);
??????????? } else if (data[j] < (startY >> 2)) {
??????????????? data[j] += (data[j] >> 1);
??????????? } else if (this.data[j] >= this.startY * 2 / 3) {
??????????????? this.data[j] = (this.startY >> 2);
??????????? }
??????? }
??????? checkWaveData();
??? }

??? private void clearWaveData() {
??????? for (int j = 0; j < this.data.length; ++j) {
??????????? this.data[j] = 0;
??????????? dataBK[j] = 0;
??????? }
??????? checkWaveData();
??? }

??? private void checkWaveData() {
??????? for (int j = 0; j < this.dataBK.length; ++j) {
??????????? this.dataBK[j] -= this.tempH1;
??????????? if (this.data[j] > this.dataBK[j]) {
??????????????? this.dataBK[j] = (this.data[j] + 1);
??????????? }
??????? }
??? }

1 楼 mvpstevenlin 2012-05-16  
试问我在Android里怎么使用呢 ?