CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2011
    Location
    Nazareth
    Posts
    5

    Question Changing The Tempo of a MIDI file

    --------------------------------------------------------------------------------

    i am trying to play a midi file using DxVBLib with C# .
    how can i change the tempo of the loaded midi file please .
    here is my small code :

    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;
    //using DxVBLib;
    using System.Runtime.InteropServices;


    using System.Threading;



    namespace htcdemo
    {
    public partial class Form1 : Form
    {
    public AudioX7 mid;

    public Form1()
    {
    InitializeComponent();
    }

    public class AudioX7
    {
    private static int port = -1;
    public static int Port
    {
    get { return port; }
    set { port = value; }
    }

    private DxVBLib.DirectX7 dx;
    private DxVBLib.DirectMusicLoader loader;
    private DxVBLib.DirectMusicPerformance performance;
    private DxVBLib.DirectMusicPerformance performance2;
    private DxVBLib.DirectMusicSegmentState segmentState;
    private DxVBLib.DirectMusicSegment segment;
    private bool fIsPaused = false;
    private double dTempo;
    private double seconds = 0;
    private long startTime = 0;
    private long mtTime = 0;
    private double mtLength;
    private long offset = 0;
    private bool playing = false;
    public double CurrentPosition
    {
    get { return this.GetCurrentPosition(); }
    set { this.SetCurrentPosition(value); }
    }
    public double Duration
    {
    get { return this.GetDuration(); }
    }

    public AudioX7()
    {
    dx = new DxVBLib.DirectX7();
    loader = dx.DirectMusicLoaderCreate();
    performance = dx.DirectMusicPerformanceCreate();
    //MessageBox.Show("");
    performance.Init(null, 0);
    performance.SetPort(Port, 16);
    performance.SetMasterAutoDownload(true);

    //Creating a Perf2 so that we can get all the segment information
    //without having to play the segment
    performance2 = dx.DirectMusicPerformanceCreate();
    performance2.Init(null, 0);
    performance2.SetPort(Port, 16);
    performance2.GetMasterAutoDownload();

    }

    public void Open(string path, string filename)
    {

    int zero = 0;
    loader.SetSearchDirectory(path);
    segment = loader.LoadSegment(filename);
    segment.SetStandardMidiFile();

    // Play the segment just long enough to get the info
    mtTime = performance2.GetMusicTime();
    performance2.PlaySegment(segment, 0, (int)mtTime + 2000);
    //MessageBox.Show(segment.ToString());
    //GetTempo
    dTempo = performance2.GetTempo((int)mtTime + 2000, ref zero);

    // Get Length
    mtLength = (((segment.GetLength() / 768) * 60) / dTempo);
    // MessageBox.Show(dTempo.ToString());
    }

    public void Play()
    {
    if (segment == null) return;
    if (fIsPaused)
    {
    offset = mtTime - startTime + offset + 1;
    segment.SetStartPoint((int)offset);
    segmentState = performance.PlaySegment(segment, 0, 0);
    playing = true;
    Thread.Sleep(90);
    }
    else
    {
    offset = 0;
    if (performance.IsPlaying(segment, segmentState) == true)
    performance.Stop(segment, segmentState, 0, 0);
    segment.SetStartPoint(0);
    segmentState = performance.PlaySegment(segment, 0, 0);
    playing = true;
    //MessageBox.Show("now pl");
    Thread.Sleep(90);
    }
    fIsPaused = false;
    }

    public void Stop()
    {
    if (segment == null) return;
    fIsPaused = false;
    if (performance != null)
    {
    performance.Stop(segment, segmentState, 0, 0);
    playing = false;
    }
    seconds = 0;
    }

    public void Pause()
    {
    //Console.WriteLine(performance.GetMusicTime());
    //Console.WriteLine(performance.GetClockTime());
    if (segment == null) return;
    if (this.Playing())
    {
    fIsPaused = true;
    mtTime = performance.GetMusicTime();
    startTime = segmentState.GetStartTime();
    performance.Stop(segment, null, 0, 0);
    playing = false;
    }
    else
    {
    fIsPaused = false;
    offset = mtTime - startTime + offset + 1;
    segment.SetStartPoint((int)offset);
    segmentState = performance.PlaySegment(segment, 0, 0);
    playing = true;
    Thread.Sleep(90);
    }
    }

    public bool Playing()
    {
    //return (performance.IsPlaying(null, segmentState));
    return playing;
    }

    public bool Paused()
    {
    return fIsPaused;
    }

    public void Close()
    {
    performance.CloseDown();
    }

    public double GetDuration()
    {
    return mtLength;
    }

    private double GetCurrentPosition()
    {
    if (performance.IsPlaying(null, segmentState))
    {
    // calculate the seconds
    seconds = ((((performance.GetMusicTime() -
    (segmentState.GetStartTime() - offset)) / 768) * 60) / dTempo);
    }
    return seconds;
    }

    public void SetCurrentPosition(double position)
    {
    offset = (long)((position * dTempo / 60) * 768 -
    performance.GetMusicTime() + segmentState.GetStartTime()) + 1;
    if (performance.IsPlaying(null, segmentState))
    {
    mtTime = performance.GetMusicTime();
    startTime = segmentState.GetStartTime();
    performance.Stop(segment, null, 0, 0);
    playing = false;
    fIsPaused = true;
    this.Play();
    }
    }
    }

    private void button1_Click(object sender, EventArgs e)
    {
    mid = new AudioX7();

    mid.Open("d:\\", "paleface.mid");
    mid.Play();
    //mid.SetCurrentPosition(20);
    //MessageBox.Show(mid.GetDuration().ToString());
    }

    private void button2_Click(object sender, EventArgs e)
    {
    mid.Pause();
    }
    }
    }

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Changing The Tempo of a MIDI file

    Please use CODE tags. It is explained here :

    http://www.codeguru.com/forum/showthread.php?t=401115

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured