Programming speaker output frequency

HI guys, i need help in my project. i am suppose to create a security system,which the speaker will give out a High frequency noise(16.4KHZ) when then Pir sensor sense a motion. Can someone help me find my mistake in this programming., cause my speaker output duration is too fast(about one second),if was suppose to give out the high frequency until no motion are detected.

 int pirpin = 2;//recieve signal from PIR
 int speakerpin = 10;//Declare the speaker pin number
 int ledpin = 13;//Declare the led pin number
void setup ()
{
  pinMode(pirpin, INPUT);//Set the sensor as input
  pinMode(ledpin, OUTPUT);//Set the LED as output
  pinMode(speakerpin, OUTPUT);//Set the speaker as output
  delay(20000);//Wait for pir to stabilize
}

void loop()
{
  
  int pirstate = digitalRead(pirpin);//Read input value
  
  if (pirstate == HIGH)//If motion detected, turns on LED and Speaker
  {
    digitalWrite(ledpin, HIGH);
    playTone(300, 16400);
    delay(30000);
    
  }
  else if (pirstate == LOW)//If no motion detected, turns off LED and speaker
  {
    digitalWrite(ledpin, LOW);
    playTone(0,0);
    delay(300);
  }
}
  void playTone(long duration, int freq)
 { 
    duration *=1000;
    int period = (1.0/freq)* 1000000;
    long elapsed_time = 0;//The period of time
    while (elapsed_time < duration)
    {
     digitalWrite(speakerpin, HIGH);
     delayMicroseconds(period /2);
     digitalWrite(speakerpin, LOW);
     delayMicroseconds(period /2);
     
     elapsed_time += (period);
  }
 }

what do you expect to hear?

playTone(0,0);

not clear from that

To turn the speaker off when no motion was detected?

classic state change...

you want to look for the change of state form LOW to HIGH or HIGH or LOW...

something like this untested...

int pirpin = 2;//recieve signal from PIR
int speakerpin = 10;//Declare the speaker pin number
int ledpin = 13;//Declare the led pin number
int state = 0;
void setup ()
{
  Serial.begin(115200);
  pinMode(pirpin, INPUT);//Set the sensor as input
  pinMode(ledpin, OUTPUT);//Set the LED as output
  pinMode(speakerpin, OUTPUT);//Set the speaker as output
  delay(20000);//Wait for pir to stabilize
}

void loop()
{
  int pirstate = digitalRead(pirpin);//Read input value
  static int lastPirState = 0;
  if (pirstate == HIGH && lastPirState == LOW)
  {
    state = 1;
    Serial.print("State = ");
    Serial.print(state);
  }
  else if (pirstate == LOW && lastPirState == HIGH)
  {
    state = 0;
    Serial.print("State = ");
    Serial.print(state);
  }
  lastPirState = pirstate;
  if (state == 1)
  {
    playTone(300, 16400);
  }
  else if (state == 0)
  {
    playTone(0,0);
  }
}
void playTone(long duration, int freq)
{ 
  duration *=1000;
  int period = (1.0/freq)* 1000000;
  long elapsed_time = 0;//The period of time
  while (elapsed_time < duration)
  {
    digitalWrite(speakerpin, HIGH);
    delayMicroseconds(period /2);
    digitalWrite(speakerpin, LOW);
    delayMicroseconds(period /2);
    elapsed_time += (period);
  }
}

One it enters the loop, won't it stay inside the loop even if the PIR output changes to LOW?

One it enters the loop, won't it stay inside the loop even if the PIR output changes to LOW?

"the loop"? Which loop are you talking about?