2
Answers

user editable sound

Photo of jonas 0

jonas 0

21y
1.8k
1
Hi, ive heard its not possible to control sound real-time using C# and DirectSound. Is there some kind of alternative? I need to create a tone generator using C# so i need to be able to play frequencies 20-20000Hz

Answers (2)

0
Photo of Vulpes
NA 98.3k 1.5m 13y
Yeah, I'm just about to have my evening meal as well.

However, it's a bit rushed but here's something for you to work on. Sam will probably be able to come up with something better later today.

Notice that as this currently stands, you have to keep the shift key pressed down to move the ship within the dock as well as to enter it.

I'm assuming you're happy with the mouse interface as it stands, because you'd need 3 hands to hold do the shift and arrow keys whilst moving the mouse:

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // move object up
            if (e.KeyCode == Keys.Up)
            {
                ship.Y = ship.Y - 5;
                if (ship.IntersectsWith(dock) && !e.Shift)
                {
                    ship.Y = ship.Y + 5;
                    MessageBox.Show("You cant enter or move within the object, shift key and arrow key must be pressed simultaneously");
                    return;
                }
                this.Refresh();
            }
            //move object down
            else if (e.KeyCode == Keys.Down)
            {
                ship.Y = ship.Y + 5;
                if (ship.IntersectsWith(dock) && !e.Shift)
                {
                    ship.Y = ship.Y - 5;
                    MessageBox.Show("You cant enter or move within the object, shift key and arrow key must be pressed simultaneously");
                    return;
                }
                this.Refresh();
            }
            // move oboject left
            else if (e.KeyCode == Keys.Left)
            {
                ship.X = ship.X - 5;
                if (ship.IntersectsWith(dock) && !e.Shift)
                {
                    ship.Y = ship.X - 5;
                    MessageBox.Show("You cant enter the object, shift key and arrow key must be pressed simultaneously");
                    return;
                }
                this.Refresh();
            }
            //move object right
            else if (e.KeyCode == Keys.Right)
            {
                ship.X = ship.X + 5;
                if (ship.IntersectsWith(dock) && !e.Shift)
                {
                    ship.Y = ship.X - 5;
                    MessageBox.Show("You cant enter the object, shift key and arrow key must be pressed simultaneously");
                    return;
                }
                this.Refresh();
            }
        }
Accepted
0
Photo of Sam Hobbs
NA 28.7k 1.3m 13y
Ooops, the calls to MoveVertical and MoveHorizontal should be calls to MoveShip. I think only the names need to be changed.
0
Photo of Sam Hobbs
NA 28.7k 1.3m 13y
If I understand Vulpes's code, then your program is moving the ship corresponding to keys pressed. I did not understand that; I had assumed that the ships were moving automatically and when there is a possible intersection your program needs to check to see if a certain key combination is pressed. Can you understand the confusion? I totally did not think that the movement could be the result of keys being pressed. I thought that the only purpose of the keys (relevant to this question) were to either allow or not allow docking. And it is not normal to hold down an arrow key so the question seemed strange to me. I thought your program would require holding down the shift key plus an arrow key just to allow docking, but the movement was being done automatically. I am being a little repititious here to help ensure I am clear.

I think the following is equivalent to Vulpes's code. Note that if it were me, I would write the code so that the program could test for intersection without updating the position; inohter words, check to determine if the new position intersects and if it does then do the move only if it is allowed. That way the position does not need to be undone if the docking is not allowed.


 const  string  CannnotMoveMessage = "You cant enter the object, shift key and arrow key must be pressed simultaneously" ;
 
 private  void  Form1_KeyDown(object  sender, KeyEventArgs e)
 {
 switch  (e.KeyCode)
 {
 case  Keys.Up:
 MoveVertical(ship.Y, - 5);
 case  Keys.Down:
 MoveVertical(ship.Y, 5);
 case  Keys.Left:
 MoveHorizontal(ship.X, - 5);
 case  Keys.Right:
 MoveHorizontal(ship.X, 5);
 }
 }
 
 void  MoveShip(int  point, int  movement)
 {
 point = point + movement;
 if  (ship.IntersectsWith(dock) && !e.Shift)
 {
 point = point - movement;
 MessageBox.Show(CannnotMoveMessage);
 return ;
 }
 this .Refresh();
 }
0
Photo of Sam Hobbs
NA 28.7k 1.3m 13y
And I want to help you do that.
0
Photo of Prime b
NA 866 249.6k 13y
Sorry Sam,from now on I will try to make my threads clearer.
0
Photo of Sam Hobbs
NA 28.7k 1.3m 13y
Please see Clear and concise questions.

Vulpes and I are different. I prefer to get clarification before proceeding; Vulpes attempts to provide an answer immediately. Providing an answer immediately works well when the question is understood and the reply is the best solution for the problem. It can happen however that the person seeking assistance goes with the first suggestion and then they are not interested in anything better. Something else that i see happening here often is that the initial guess is incorrect, so a long discussion erupts in which a suggestion is made and then a little bit more of the requirements are determined and then another suggestion is attempted. I do not like to do that because it wastes time. What I like to do is to get the requirements clarified as much as possible first without investing time in the actual source code. It is frustrating for me, since if I spend time clarifying something, then someone else can use the results of my time and post sample source code. So even if I spend more time, the other person gets credit. When I say credit, I do not mean points; I mean that people get the impression that I am not trying to help. People confuse my attempts to get clarification as if they are answers.

Programmers need to learn that if they understand the problem, the solution is often easy. Many members here do not understand that. When the reply to a vague question is sample source code, the person seeking assistance does not learn how to analyze the problem.

I will try to look at this more, but emotionally it is very difficult for me.
0
Photo of Prime b
NA 866 249.6k 13y
haha 3 hands.
I just tried to make two different ways to move the ship, 1. you can use only mouse or 2. you can use arrow keys, but to make it more difficult the shift key should be held down to enter the dock( it just goes inside of the object ).
I dont have VS installed here, so once i get home I will see if it works.
Thank you!
0
Photo of Sam Hobbs
NA 28.7k 1.3m 13y
I have work to do at the moment. I have not even had breakfast yet.
0
Photo of Prime b
NA 866 249.6k 13y
Is my question not clear or something?
0
Photo of Prime b
NA 866 249.6k 13y
anyway, where did my friend vulpes go?!

0
Photo of Prime b
NA 866 249.6k 13y
What I mean when one object touches the borders of the other(or goes inside of other object). The question is, how do I make one object to enter the other only when shift and one of the arrows keys pushed down else error message displayed saying the shift or arrow key is not pressed. Tell me if you do understand now
0
Photo of Sam Hobbs
NA 28.7k 1.3m 13y
I am sorry but I do not understand. For example, I do not know if "pressed" means held down or not. Also I do not understand what "two object can interact with each other" means; do you mean "to object and interact with each other", as in a battle?

Actually, I do not see a question here.