|
การตัดเพลงซ้ำ MIDI Karaoke แบบบ้านๆ 1 |
หลังจากจัดการกับ NCN Karaoke แบบบ้านๆแล้ว
https://www.thaicreate.com/community/ncn-move-ban-ban.html
ก็ถึงเวลาตัดเพลงซ้ำครับ
หลักการของผมก็บ้านๆจริงๆครับ
1. select database โดยให้เรียง ARTIST + TITLE ลงใน datagrid
2. ไล่หาเพลงซ้ำทีละ row (บอกแล้วบ้านๆจริงๆ)
หน้าตาเป็นแบบนี้ครับ
Form1.Designer.cs
Code (C#)
namespace MIDI_Duplicatesong
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.sONGDataGridView = new System.Windows.Forms.DataGridView();
this.panel1 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.progressBar2 = new System.Windows.Forms.ProgressBar();
this.label3 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.sONGDataGridView)).BeginInit();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// sONGDataGridView
//
this.sONGDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.sONGDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
this.sONGDataGridView.Location = new System.Drawing.Point(0, 218);
this.sONGDataGridView.Name = "sONGDataGridView";
this.sONGDataGridView.Size = new System.Drawing.Size(1017, 273);
this.sONGDataGridView.TabIndex = 11;
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.progressBar2);
this.panel1.Controls.Add(this.label3);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.textBox1);
this.panel1.Controls.Add(this.button1);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(1017, 218);
this.panel1.TabIndex = 10;
//
// button2
//
this.button2.Location = new System.Drawing.Point(841, 32);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(43, 23);
this.button2.TabIndex = 37;
this.button2.Text = "run";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// progressBar2
//
this.progressBar2.Dock = System.Windows.Forms.DockStyle.Bottom;
this.progressBar2.Location = new System.Drawing.Point(0, 199);
this.progressBar2.Name = "progressBar2";
this.progressBar2.Size = new System.Drawing.Size(1017, 19);
this.progressBar2.TabIndex = 36;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(13, 42);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(28, 13);
this.label3.TabIndex = 6;
this.label3.Text = "Test";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(13, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(69, 13);
this.label1.TabIndex = 2;
this.label1.Text = "PathKaraoke";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 6);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(747, 20);
this.textBox1.TabIndex = 1;
//
// button1
//
this.button1.Location = new System.Drawing.Point(841, 3);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(43, 23);
this.button1.TabIndex = 0;
this.button1.Text = "...";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1017, 491);
this.Controls.Add(this.sONGDataGridView);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.sONGDataGridView)).EndInit();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView sONGDataGridView;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar progressBar2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
}
}
อันดับแรกก็ต้องมา SetData ก่อนครับ
ไฟล์ database คือ SONG.DBF อยู่ใน folder karaoke\Data
Code (C#)
string PathKARA = "";
//open folder karaoke
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fol = new FolderBrowserDialog();
fol.ShowDialog();
textBox1.Text = fol.SelectedPath;
PathKARA = textBox1.Text;
new System.Threading.Thread(SetData).Start();
}
// SetData
void SetData()
{
this.Invoke(new Action(() => { Text = "Adding Song"; }));
string PathData = PathKARA + "\\DATA";
if (!System.IO.Directory.Exists(PathData)) { MessageBox.Show("ไม่พบ Song Data"); return; }
System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection("Dsn=dBASE Files;dbq=" + PathData.ToUpper() + ";defaultdir=" + PathData.ToUpper() + ";driverid=533;maxbuffersize=2048;pagetimeout=5");
conn.Open();
string strQuery = "SELECT CODE,TYPE,SUB_TYPE,ARTIST + TITLE as SongName FROM SONG where TYPE like '%MIDI%' order by ARTIST + TITLE";
System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
sONGDataGridView.Invoke(new Action(() => { sONGDataGridView.DataSource = ds.Tables[0].DefaultView; }));
this.Invoke(new Action(() => { Text = "All Song:" + (sONGDataGridView.RowCount - 1); }));
}
เราจะจัดการเฉพาะ ไฟล์ ใน MIDI\NCN และ MIDI\EMK
ดังนั้นผมจะใช้ where TYPE like '%MIDI%'
และ order by ARTIST + TITLE" (รวม ARTIST และ TITLE) ไว้ใน คอลัมน์เดียวกันครับ
จะได้ตรวจสอบง่ายขึ้น
ส่วนการตรวจสอบ
จะมีทั้งที่เป็น EMK และ NCN(ซึ่งจะมี ไฟล์ .mid .cur .lyr)
โดยดูจาก SUB_TYPE ครับ ว่าเป็น EMK หรือ NCN
ผมจะแบ่งการทำงานเป็น Thread นะครับ
เพื่อเพิ่มความเร็วในการทำงาน จะได้ทำงานทีละหลาย Thread ครับ
คอมผมรุ่นเก่า ขอใช้ int maxThread = 20; ละกันครับ
แล้วก็ส่งค่าให้กับ CheckFile เพื่อทำการลบเพลงซำ้ครับ
Code (C#)
private void button2_Click(object sender, EventArgs e)
{
startCounter = DateTime.Now.TimeOfDay.TotalSeconds;
int maxThread = 20;
int sngPer = (int)(sONGDataGridView.RowCount - 1) / maxThread;
progressBar2.Invoke(new Action(() => { progressBar2.Maximum = this.sONGDataGridView.RowCount - 1; }));
startCounter = DateTime.Now.TimeOfDay.TotalSeconds;
for (int i = 0; i < maxThread; i++) {
new System.Threading.Thread(CheckFile).Start(new object[] { i * sngPer, ((i + 1) * sngPer -1) }); }
}
เอาละครับโค้ดหน้า Form1 จะเป็นแบบนี้ครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MIDI_Duplicatesong
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int sngDel = 0, sngRun = 0;
string PathKARA = "";
double startCounter;
#region _function
void SetData()
{
this.Invoke(new Action(() => { Text = "Adding Song"; }));
string PathData = PathKARA + "\\DATA";
if (!System.IO.Directory.Exists(PathData)) { MessageBox.Show("ไม่พบ Song Data"); return; }
System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection("Dsn=dBASE Files;dbq=" + PathData.ToUpper() + ";defaultdir=" + PathData.ToUpper() + ";driverid=533;maxbuffersize=2048;pagetimeout=5");
conn.Open();
string strQuery = "SELECT CODE,TYPE,SUB_TYPE,ARTIST + TITLE as SongName FROM SONG where TYPE like '%MIDI%' order by ARTIST + TITLE";
System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
sONGDataGridView.Invoke(new Action(() => { sONGDataGridView.DataSource = ds.Tables[0].DefaultView; }));
this.Invoke(new Action(() => { Text = "All Song:" + (sONGDataGridView.RowCount - 1); }));
}
string Time(double d)
{
string days = (((int)(d / 3600) / 24) > 0) ? ((int)(d / 3600) / 24) + "days:" : "";
string hours = ((int)(d / 3600) % 24 > 0) ? (int)(d / 3600) % 24 + "hours:" : "";
string minutes = ((int)(d / 60) % 60 > 0) ? (int)(d / 60) % 60 + "minutes:" : "";
string seconds = ((int)d % 60 > 0) ? ((int)d % 60) + "seconds" : "";
return days.ToString() + hours.ToString() + minutes.ToString() + seconds.ToString();
}
private void CheckFile(object obj)
{
string cri = "";
int min = (int)((object[])obj)[0];
int max = (int)((object[])obj)[1];
for (int i = min; (i < max && i < sONGDataGridView.RowCount); i++)
{
if (this.sONGDataGridView[3, i].Value.ToString().Trim().Replace(" ", "").ToUpper() != cri)
{
cri = this.sONGDataGridView[3, i].Value.ToString().Trim().Replace(" ", "").ToUpper();
}
else
{
sngDel++;
if (this.sONGDataGridView[2, i].Value.ToString() == "EMK")
{
MoveFile(PathKARA + "\\SONGS\\MIDI\\EMK\\" + this.sONGDataGridView[0, i].Value.ToString().Substring(0, 1) + "\\" + this.sONGDataGridView[0, i].Value.ToString() + ".emk");
}
else if (this.sONGDataGridView[2, i].Value.ToString() == "NCN")
{
MoveFile(PathKARA + "\\SONGS\\MIDI\\NCN\\Lyrics\\" + this.sONGDataGridView[0, i].Value.ToString().Substring(0, 1) + "\\" + this.sONGDataGridView[0, i].Value.ToString() + ".lyr");
MoveFile(PathKARA + "\\SONGS\\MIDI\\NCN\\Cursor\\" + this.sONGDataGridView[0, i].Value.ToString().Substring(0, 1) + "\\" + this.sONGDataGridView[0, i].Value.ToString() + ".cur");
MoveFile(PathKARA + "\\SONGS\\MIDI\\NCN\\Song\\" + this.sONGDataGridView[0, i].Value.ToString().Substring(0, 1) + "\\" + this.sONGDataGridView[0, i].Value.ToString() + ".mid");
}
}
try
{
if (sngRun > 0)
{
double t = DateTime.Now.TimeOfDay.TotalSeconds - startCounter;
double strt = (double)((this.sONGDataGridView.RowCount - sngRun) * t / sngRun);
this.label3.Invoke(new Action(() =>
{
this.label3.Text = "Cri =" + cri + Environment.NewLine +
"Check code " + this.sONGDataGridView[0, i].Value.ToString() + this.sONGDataGridView[2, i].Value.ToString() + Environment.NewLine +
"Delete file;" + sngDel + Environment.NewLine +
"File: " + sngRun + "/" + (this.sONGDataGridView.RowCount - 1) + Environment.NewLine +
"Completion Ratio:" + string.Format("{0:0.0000}", Convert.ToDouble(100 * (double)sngRun / (double)(this.sONGDataGridView.RowCount - 1))) + " %" + Environment.NewLine +
"Speed:" + string.Format("{0:0.00}", ((double)sngRun / t)) + "file/s" + Environment.NewLine +
"Elapsed time:" + Time(t) + Environment.NewLine +
"Remaining time:" + Time(strt);
}));
}
this.progressBar2.Invoke(new Action(() => { this.progressBar2.Value = sngRun; }));
}
catch { }
System.Threading.Thread.Sleep(50);
sngRun++;
}
}
string PathKaraTemp()
{
if (!(System.IO.Directory.Exists(System.IO.Path.GetTempPath() + "\\KaraTemp")))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetTempPath() + "\\KaraTemp");
}
return System.IO.Path.GetTempPath() + "\\KaraTemp";
}
void MoveFile(string _FileName)
{
try { System.IO.File.Delete(_FileName); }
catch
{
try
{
System.IO.File.Move(_FileName, PathKaraTemp() + "\\" + String.Format("{0:ddMMyyyyHHmmss}", DateTime.Now) + System.IO.Path.GetFileName(_FileName));
}
catch { }
}
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fol = new FolderBrowserDialog();
fol.ShowDialog();
textBox1.Text = fol.SelectedPath;
PathKARA = textBox1.Text;
new System.Threading.Thread(SetData).Start();
}
private void button2_Click(object sender, EventArgs e)
{
startCounter = DateTime.Now.TimeOfDay.TotalSeconds;
int maxThread = 20;
int sngPer = (int)(sONGDataGridView.RowCount - 1) / maxThread;
progressBar2.Invoke(new Action(() => { progressBar2.Maximum = this.sONGDataGridView.RowCount - 1; }));
startCounter = DateTime.Now.TimeOfDay.TotalSeconds;
for (int i = 0; i < maxThread; i++) { new System.Threading.Thread(CheckFile).Start(new object[] { i * sngPer, ((i + 1) * sngPer -1) }); }
}
}
}
และแล้วเราก็เอาโค้ดบ้านๆมาใช้งาน ตัดเพลงซ้ำ MIDI Karaoke แบบบ้านๆได้แล้วครับ
ครั้งต่อไปก็มาจัดการไฟล์ VEDIO กันนะครับขอแก้โค้ดให้นิ่งๆบ้านๆแบบใครหยิบใช้ก็ใช้ได้ซักหน่อยครับ
เดี๋ยวเอามาแบ่งให้ใช้กันครับ
|
|
|
|
|
|
|
|
By : |
TOR_CHEMISTRY
|
|
Article : |
บทความเป็นการเขียนโดยสมาชิก หากมีปัญหาเรื่องลิขสิทธิ์ กรุณาแจ้งให้ทาง webmaster ทราบด้วยครับ |
|
Score Rating : |
|
|
Create Date : |
2016-01-28 |
|
Download : |
No files |
|
Sponsored Links |
|
|
|
|
|
|